I am writing test code with xUnit and mock(moq) on C#.
However, the program through the interface cannot be executed.
The test code is as follows.
// test code
public void test1(string id, EntityA fd)
{
var mockOrder = new Mock<InterfaceOrder>();
var service = new ItemService(mockOrder.Object);
var result = service.FunctionA(id, fd);
Assert.False(result);
}
// service
public class ItemService(InterfaceOrder order)
{
public bool FunctionA(string id, EntityA fd)
{
if (id == null) return false;
// ↓ GetList() is not executed and does not return the expected list
var list = order.GetList(id).ToList();
return list.Count() > 0 ? true : false;
}
}
// Interface Definition
public interface InterfaceOrder
{
IEnumerable<SalesEntity> GetList(string id);
}
// Interface Facts
public class EntityFrameOrder : InterfaceOrder
{
private readonly salesContext _context;
public EntityFrameOrder(IOptions<ConnectionString> connection)
{
var connectionString = connection.Value.Path;
DbContextOptionsBuilder<salesContext> opt = new();
opt.UseOracle(connectionString);
_context = new salesContext(opt.Options);
}
public IEnumerable<SalesEntity> GetList(string id)
{
var query = $@"
SELECT *
FROM SALES
WHERE CUSTOMERCD = :id
";
try
{
using DbManager dbManager = new();
~
return list;
}
}
}
GetList() of FunctionA of ItemService is not executed and does not return the expected result.
I am using mock, but do we also need a mock instance of context?
If you have any tips to give me, please let me know.
Thank you.
EntityFrameOrderhas no business configuring a DbContext and the vendor-specific SQL shouldn't be used at all.context.Sales.First(s=>s.CustomerCD=someId)should be used instead. The DbContext should be configured outside the DbContext/data layer, whatever. Preferably inProgram.cs, as all examples and tutorials show. This would allow EF to work with any database without the rest of the application noticing itGetListisn't executed? It's a mock, so unless you provide some implementation for that function in your mocksSetup, it won't do anything. Or in other words: your mocked object has nothing to do with yourEntityFrameOrder-class, that's the entire purpose of a mock in the first place.GetListitself and configure it to return a list of items. If you want to mock the UoW/DbContext, 1) don't and 2) configure the DbContext class to use either the in-memory provider or SQLite in memory mode.