pengweiqhca / Xunit.DependencyInjection

Use Microsoft.Extensions.DependencyInjection to resolve xUnit test cases.
MIT License
370 stars 49 forks source link

Unable to inject dependency into test class #5

Closed iFalcao closed 5 years ago

iFalcao commented 5 years ago

Hello, I was unable to find the ConfigureServices method as specified in README, so I'm using the method without a return:

public class Startup : DependencyInjectionTestFramework
    {
        public Startup(IMessageSink messageSink) : base(messageSink) { }

        protected override void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<ICityService, CityService>();
            services.AddTransient<ICityRepository, CityRepository>();
            services.AddDbContext<DataContext>(x =>
               x.UseSqlite("Data Source=PrevisaoTempoTest.db")
            );
            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

            services.BuildServiceProvider();
        }
    }

When I try to run the test, I get the following error:

Result Message: The following constructor parameters did not have matching fixture data: ICityRepository cityRepository

Here is my test class:

public class CityServiceTest
    {
        private readonly ICityRepository _cityRepository;

        public CityServiceTest(ICityRepository cityRepository)
        {
            _cityRepository = cityRepository;
        }

        [Fact]
        public void InsertCorrect()
        {
            var newCity = new City
            {
                Name = "Chicago"
            };
            this._cityRepository.Insert(newCity);

            var cityInserted = this._cityRepository.SelectAll().First();
            Assert.True(cityInserted.Name == newCity.Name);
            Assert.True(cityInserted.CustomCode == null);
            Assert.True(cityInserted.Id == 1);
        }
    }

Any ideas of why this isn't working?

iFalcao commented 5 years ago

Resolved by adding the Microsoft.AspNetCore.Mvc nuget package in the project.