我遇到了一些FakeItEasy的奇怪問題。FakeItEasy沒有找到通話雖然它有
想象一下以下的單元測試方法:
[TestMethod]
public void DeletePerson_WhenCalled_ThenPersonIsDeleted()
{
const int personId = 24;
var commonParam = new CommonParam();
this.testee.DeletePerson(commonParam, personId);
A.CallTo(() => this.personRepository.DeletePersons(commonParam, new[] {personId }, false)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => this.personRepository.SaveChanges()).MustHaveHappened(Repeated.Exactly.Once);
}
的testee.DeletePerson
- 方法是這樣的:
public ResultatModel DeletePerson(CommonParam commonParam, int personId)
{
this.personRepository.DeletePersons(commonParam, new[] { personId });
this.personRepository.SaveChanges();
}
而且personRepository.DeletePersons
(但這個由fakeiteasy僞造...):
public void DeletePersons(CommonParam commonParam, IEnumerable<int> ids, bool hardRemove = false)
{
var persons = Entities.per_person.Where(e => ids.Contains(e.personId)
&& (e.accountId == null || e.accountId == commonParam.AccountId)).ToList();
if (hardRemove)
{
Entities.per_person.RemoveRange(persons);
}
else
{
persons.ForEach(person =>
{
person.geloescht = true;
person.mutationsBenutzer = commonParam.DbIdent;
person.mutationsDatum = DateTime.Now;
});
}
}
這就是測試失敗的原因
試驗方法DataService.Test.PersonServiceTest.DeletePerson_WhenCalled_ThenPersonIsDeleted拋出異常: FakeItEasy.ExpectationException:
斷言失敗以下調用: RepositoryContract.IPersonRepository.DeletePersons(commonParam:Commons.CommonParam,IDS:系統。 INT32 [],hardRemove:假) 預期找到它正好一次,但發現它#0次呼叫中: 1:RepositoryContract.IPersonRepository.RequestInfo =僞造的Commons.Session.RequestInfo 2:RepositoryContract.IPersonRepository.DeletePersons( commonParam :Commons.CommonParam, IDS:System.Int32 [], hardRemove:假) 3:RepositoryContract.IPersonRepository.SaveChanges()
爲什麼測試失敗?
是new[] { ... }
有問題嗎?
在此先感謝
嗯,你確定嗎?我的意思是'new [] {...}'是'IEnumerable'也不是?我嘗試將調用改爲'新列表 {...}',但它仍然不起作用:-( –
xeraphim
它在運行時,它不在編譯時間中。將其中任何其他類型。 – dbencs