使用 Moq 和 NUnit ,什么是编写方法/更好的语法?
我正在尝试测试这种行为
- BLOGTableAdapter.GetBlogsByTitle(string title) 被调用并且只调用一次
——并且用具有以下内容的字符串调用 长度大于 1,
-- 它返回 BLOGDataTable 对象
[Test]
public void GetBlogsByBlogTitleTest4()
{
var mockAdapter = new Mock<BLOGTableAdapter>();
var mockTable = new Mock<BLOGDataTable>();
mockAdapter.Setup(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0))).Returns(mockTable.Object);
var blogBl = new BlogManagerBLL(mockAdapter.Object);
blogBl.GetBlogsByBlogTitle("Thanks for reading my question");
mockAdapter.VerifyAll();
mockAdapter.Verify(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0)), Times.Exactly(1));
}
当调用 GetBlogsByTitle(string title) 时,在类中的数据访问层中说“BlogManagerBLL”
public BLOGDataTable GetBlogsByBlogTitle(string title)
{
return Adapter.GetBlogsByTitle(title);
}
如您所见,我使用两个单独的语句来完成这些检查
mockAdapter.Setup(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0))).Returns(mockTable.Object);
mockAdapter.Verify(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0)), Times.Exactly(1));
- 我怎样才能把它变成一个 陈述 ?
- 我测试的东西正确吗?
谢谢
I am trying to test this behavior
-- BLOGTableAdapter.GetBlogsByTitle(string
title) is called and for once only
-- and is called with string having
length greater than 1,
-- and it returns BLOGDataTable Object
[Test]
public void GetBlogsByBlogTitleTest4()
{
var mockAdapter = new Mock<BLOGTableAdapter>();
var mockTable = new Mock<BLOGDataTable>();
mockAdapter.Setup(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0))).Returns(mockTable.Object);
var blogBl = new BlogManagerBLL(mockAdapter.Object);
blogBl.GetBlogsByBlogTitle("Thanks for reading my question");
mockAdapter.VerifyAll();
mockAdapter.Verify(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0)), Times.Exactly(1));
}
When a calls is made to GetBlogsByTitle(string title), in class say "BlogManagerBLL" in Data Aceess Layer
public BLOGDataTable GetBlogsByBlogTitle(string title)
{
return Adapter.GetBlogsByTitle(title);
}
As you can see, I am using two seperate statements to get these checks done
mockAdapter.Setup(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0))).Returns(mockTable.Object);
mockAdapter.Verify(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0)), Times.Exactly(1));
- How can I put this into one
statement ? - Am I testing right things ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要测试两件事,则应该编写两个测试。
所以
我想我的建议是不要将它们放在一起,为每个创建一个测试。我想说你正在测试正确的东西。
If you're testing two things, you should write two tests.
and
So I guess my advice is don't put them together, create a test for each. And I'd say you're testing the right things.