У меня есть проект, в котором мне нужно использовать базу данных (sqlexpress база данных) с entity framework, и мне нужно сделать для нее модульные тесты, на моем компьютере и компьютерах моей команды эти тесты работают, но на github ( тест рабочего процесса или что-то в этом роде) эти тесты не работают. Это потому, что на github, насколько мне известно, тесты запускаются на машине ubuntu. Из-за этого sqlexpress не работает, поэтому мой вопрос: как я могу изменить свой код, чтобы он тестировал то же самое, но использовал sql lite из базы данных памяти, чтобы тесты работали онлайн
Это это мой тест
public class CompaniesControllerTest
{
//fake test entities
private AddressDetail[] fakeAddressDetails = new[] {
new AddressDetail {
Id = Guid.Parse("7DBBA9EF-97E6-4D06-A6CC-E2426C170780"),
AddressLine = "Steenweg op Etterb. 180",
Postal = "1040",
City = "Brussel",
IsDeleted = false,
CreationDate = DateTime.Now
},
new AddressDetail
{
Id = Guid.Parse("A4E33A9F-D767-42F0-895A-E68472724BAB"),
AddressLine = "Steenweg op Etterb. 180",
Postal = "1040",
City = "Brussel",
IsDeleted = false,
CreationDate = DateTime.Now
},
new AddressDetail
{
Id = Guid.Parse("63E7436F-9A0E-447E-85BD-1B17876B44D1"),
AddressLine = "Steenweg op Etterb. 180",
Postal = "1040",
City = "Brussel",
IsDeleted = false,
CreationDate = DateTime.Now
},
new AddressDetail
{
Id = Guid.Parse("244C084F-BC5A-4865-8713-A5C2C1F15586"),
AddressLine = "Steenweg op Etterb. 180",
Postal = "1040",
City = "Brussel",
IsDeleted = false,
CreationDate = DateTime.Now
},
new AddressDetail
{
Id = Guid.Parse("63D85903-9062-4595-9468-FBA62171B4B6"),
AddressLine = "Steenweg op Etterb. 180",
Postal = "1040",
City = "Brussel",
IsDeleted = false,
CreationDate = DateTime.Now
},
new AddressDetail
{
Id = Guid.Parse("9F36B357-8C90-4643-AC0D-93DC98D23185"),
AddressLine = "Steenweg op Etterb. 180",
Postal = "1040",
City = "Brussel",
IsDeleted = false,
CreationDate = DateTime.Now
},
new AddressDetail
{
Id = Guid.Parse("62C53856-FBBF-4731-BDDF-C66F77A3321D"),
AddressLine = "Steenweg op Etterb. 180",
Postal = "1040",
City = "Brussel",
IsDeleted = false,
CreationDate = DateTime.Now
}
};
private Company[] fakeCompanies = new[]
{
new Company { Id = Guid.Parse("C5E36951-7223-454D-9FA8-4E63E26AB264") , Name = "Google", VAT = "054987846198", AddressDetails = new AddressDetail {Id = Guid.Parse("2B2EA755-79AD-462A-B132-26B056480F75"), AddressLine = "Steenweg op Etterb. 180", Postal = "1040", City = "Brussel", IsDeleted = false, CreationDate = DateTime.Now}, IsDeleted = false , CreationDate = DateTime.Now },
new Company { Id = Guid.Parse("5EAD5F07-2578-4AFC-A52C-C67E8FBC8112"), Name = "Amazon", VAT = "054987846199", AddressDetails = new AddressDetail {Id = Guid.Parse("B4BA1471-4D77-405F-A2CB-5CB0229379B1"), AddressLine = "Steenweg op Etterb. 180", Postal = "1040", City = "Brussel", IsDeleted = false, CreationDate = DateTime.Now}, IsDeleted = false, CreationDate = DateTime.Now }
};
private Project[] fakeProjects = new Project[]
{
new Project
{Id = Guid.Parse("6E8CB590-4CAC-49D2-A532-36222DEA7C83"),
Name = "project 2",
Description = "project 1",
IsDeleted = false,
CreationDate = DateTime.Now
},
new Project
{
Id = Guid.Parse("9E739A70-3E68-436B-AF47-60129F6DC960"),
Name = "project 3",
Description = "project 1",
IsDeleted = false,
CreationDate = DateTime.Now
},
new Project
{
Id = Guid.Parse("B1C6778D-D09B-495C-9F8B-7D0AD87DE6AF"),
Name = "project 4",
Description = "project 1",
IsDeleted = false,
CreationDate = DateTime.Now
},
new Project
{
Id = Guid.Parse("2CF622F0-4020-45B0-B290-F11C9B3E7803"),
Name = "project 5",
Description = "project 1",
IsDeleted = false,
CreationDate = DateTime.Now
},
new Project
{
Id = Guid.Parse("2438197A-B657-4E45-9D5F-0FD8B342F50A"),
Name = "project 6",
Description = "project 1",
IsDeleted = false,
CreationDate = DateTime.Now
}
};
[Fact]
public void Index_ReturnsCorrectCompanies()
{
/*
* ARRANGE
*/
var fakeContext = CreateContextMock();
var controller = new CompaniesController(fakeContext.Object);
var result = controller.Index();
var viewResult = Assert.IsType<ViewResult>(result);
var model = Assert.IsAssignableFrom<CompanyIndexViewModel>(viewResult.ViewData.Model);
Assert.Equal(fakeCompanies.Length, model.Companies.Count());
Assert.Subset(fakeCompanies.ToHashSet(), model.Companies.ToHashSet());
}
private Mock<GradeContext> CreateContextMock()
{
// mock dbsets
var mockCompaniesSet = CreateDbSetMock(fakeCompanies);
var mockAddressDetailsSet = CreateDbSetMock(fakeAddressDetails);
var mockProjectsSet = CreateDbSetMock(fakeProjects);
// mock context
var mockContext = new Mock<GradeContext>();
mockContext.Setup(mock => mock.Companies)
.Returns(mockCompaniesSet.Object);
mockContext.Setup(mock => mock.AddressDetails)
.Returns(mockAddressDetailsSet.Object);
mockContext.Setup(mock => mock.Projects)
.Returns(mockProjectsSet.Object);
return mockContext;
}
private Mock<DbSet<T>> CreateDbSetMock<T>(IEnumerable<T> elements) where T : class
{
var elementsAsQueryable = elements.AsQueryable();
var dbSetMock = new Mock<DbSet<T>>();
dbSetMock.As<IQueryable<T>>().Setup(m => m.Provider).Returns(elementsAsQueryable.Provider);
dbSetMock.As<IQueryable<T>>().Setup(m => m.Expression).Returns(elementsAsQueryable.Expression);
dbSetMock.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(elementsAsQueryable.ElementType);
dbSetMock.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(elementsAsQueryable.GetEnumerator());
return dbSetMock;
}
}
Это метод индексации от контроллера компании, который я хочу протестировать
private GradeContext gradeContext;
public CompaniesController(GradeContext context)
{
gradeContext = context;
}
public IActionResult Index()
{
var model = new CompanyIndexViewModel();
model.Companies = gradeContext.Companies
.Where(x => x.IsDeleted == false)
.ToList();
model.AddressDetails = gradeContext.AddressDetails
.Where(x => x.IsDeleted == false)
.ToList();
model.Projects = gradeContext.Projects
.Where(x => x.IsDeleted == false)
.ToList();
return View(model);
}
Есть способ проверить это без использования макета