-1
我试图建立一种使用一些虚拟数据来测试我的服务层(&存储库)的方法。我以前在Generic Repositories中见过这方面的例子,但我在使用DatabaseFactory时努力工作。NUnit - 模拟存储库和虚拟数据测试
当我从repository.Object调用GetPhrase方法时,我每次都会返回null。
我正在使用NUnit和Moq。任何关于我要去哪里的错误的指针将不胜感激,或者让我知道,如果我最好走下另一条路 连接到本地数据库进行测试(SQL CE等)
这里是代码的主要组成部分:在您的测试调用phraseRepository.Object.GetPhrase("H300")
总是返回null
,除非你将它设置为返回不同的东西
public class PhraseRepository : RepositoryBase<Phrase>, IPhraseRepository
{
public PhraseRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)
{
}
public string GetPhrase(string phraseCode)
{
return this.GetMany(p => p.PhraseCode == phraseCode).First().Descript;
}
}
public interface IPhraseRepository : IRepository<Phrase>
{
string GetPhrase(string phraseCode);
}
public class CLPRiskPhraseService : ICLPRiskPhraseService
{
private readonly IPhraseRepository phraseRepository;
public string GetPhrase(string phraseCode)
{
return phraseRepository.GetPhrase(phraseCode);
}
}
[Test]
public void GetPhrase()
{
var phrases = new FakePhraseData().GetPhrases();
phraseRepository.Setup(m => m.GetMany(It.IsAny<Expression<Func<Phrase, bool>>>())).Returns(phrases);
var result = phraseRepository.Object.GetPhrase("H300");
// Assert
NUnit.Framework.Assert.IsNotNull(phraseRepository.Object);
NUnit.Framework.Assert.AreEqual("Description0", result);
}