我使用.NET 4,NUnit和Rhino mocks。我想单元测试我的新闻库,但我不确定如何去做。我的新闻库是我最终将用来与数据库进行通信的。我想用它来测试假冒伪造数据。不确定是否有可能?这是我目前有:单元测试库需要的帮助/建议
public interface INewsRepository
{
IEnumerable<News> FindAll();
}
public class NewsRepository : INewsRepository
{
private readonly INewsRepository newsRepository;
public NewsRepository(INewsRepository newsRepository)
{
this.newsRepository = newsRepository;
}
public IEnumerable<News> FindAll()
{
return null;
}
}
我的单元测试是这样的:
public class NewsRepositoryTest
{
private INewsRepository newsRepository;
[SetUp]
public void Init()
{
newsRepository = MockRepository.GenerateMock<NewsRepository>();
}
[Test]
public void FindAll_should_return_correct_news()
{
// Arrange
List<News> newsList = new List<News>();
newsList.Add(new News { Id = 1, Title = "Test Title 1" });
newsList.Add(new News { Id = 2, Title = "Test Title 2" });
newsRepository.Stub(r => r.FindAll()).Return(newsList);
// Act
var actual = newsRepository.FindAll();
// Assert
Assert.AreEqual(2, actual.Count());
}
}
在上面的代码,我不知道我需要模拟。上面的代码编译但在NUnit GUI中关于构造器值失败。我只能假设它与我需要提供给NewsRepository的INewsRepository参数有关。我不知道如何在测试中做到这一点。有人可以纠正我的单元测试,以便它会通过NUnit GUI?某人是否也可以提供一些关于我是否正确实施存储库的反馈?
作为一个新手嘲笑,有什么我需要验证?我什么时候需要验证?它的目的是什么?我一直在努力完成一些源代码项目,一些使用验证,一些则不使用。
如果上述测试通过,这对开发者证明了什么?另一个开发人员必须对我的存储库做些什么才能使其在NUnit GUI中失败?
对不起,所有的问题,但他们是新手问题:)
我希望soomeone能帮助我。
我看到对不对? NewsRepository本身具有相同的功能? – Aliostad 2010-12-09 13:36:29
@Aliostad:你是什么意思? – 2010-12-09 13:39:15