'Nunit Test Dependent Commits - By ID
I have a method that does two commits - here is a simplified version of it:
public void methodA(objectA objA, List<objectB> objB)
{
_repoA.AddAsync(objA);
var id = _repoA.SaveChanges();
if(id > 0) {
foreach (var ob in objB)
{
ob.objAId = id;
_repoB.AddRange(ob);
}
_repoB.SaveChanges();
}
}
How do I get/set the id in my unit test to see if my list's objAId is set and that it actually contains two object - right now as I cannot set/get id in first commit my test fails:
[Fact]
public async void test()
{
var objA = new ObjA(null);
var objB = new List<ObjB>
{
new ObjB(null, null, "Test"),
new ObjB(null, null, "Test2")
});
var result = await _sut.methodA(objA, objB);
Assert.NotNull(result);
_mockFieldRepository.Verify(x => x.InsertAsync(It.IsAny<MyObjectA>()), Times.Once);
_mockFieldOptionRepository.Verify(x => x.InsertAsync(It.IsAny<MyObjectB>()), Times.Once); //fails here
_mockFieldOptionRepository.Verify(x => x.InsertAsync(It.Is<MyObjectB>(p => p.Id > 0 && p.objAId == objA.Id))); //fails here
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
