.Net Core 2.0 콘솔 프로그램에서 DBContext를 설정하는 데 다음 코드가 있으며 주 응용 프로그램 클래스의 생성자에 주입됩니다.xUnit 테스트 프로젝트에서 DbContext를 올바르게 설정하는 방법은 무엇입니까?
IConfigurationRoot configuration = GetConfiguration();
services.AddSingleton(configuration);
Conn = configuration.GetConnectionString("ConnStr1");
services.AddDbContext<MyDbContext>(o => o.UseSqlServer(Conn));
이제 xUnit 테스트 클래스를 만들고 테스트를 위해 동일한 DbContext를 초기화해야합니다.
context = new MyDbContext(new DbContextOptions<MyDbContext>());
그것은 null 일 수 없습니다 매개 변수 connectionString
의 오류를 가져옵니다. 테스트 프로젝트에서 DbContext를 올바르게 설정하는 방법은 무엇입니까?
당신은 그냥 옵션을 설정해야합니다. 예를 들어이 옵션은 db를 메모리에 저장하는 데 사용됩니다 : 'var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseInMemoryDatabase(); var context = new MyDbContext (optionsBuilder.Options);' –