使用 Moq 和 NUnit ,什么是编写方法/更好的语法?

发布于 2024-08-18 02:48:19 字数 1380 浏览 5 评论 0原文

我正在尝试测试这种行为

- 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));
  1. 我怎样才能把它变成一个 陈述 ?
  2. 我测试的东西正确吗?

谢谢

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));
  1. How can I put this into one
    statement ?
  2. Am I testing right things ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2024-08-25 02:48:19

如果您要测试两件事,则应该编写两个测试。

[Test]
public void BlogTableAdapter_should_be_called_with_string_having_length_greater_than_1()
{
    var mockAdapter = new Mock<BLOGTableAdapter>();

    var blogBl = new BlogManagerBLL(mockAdapter.Object);
    blogBl.GetBlogsByBlogTitle("Thanks for reading my question");

    mockAdapter.Verify(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0)));
}

所以

[Test]
public void BlogTableAdapter_should_return_a_BLOGDataTable_object()
{
    var mockAdapter = new Mock<BLOGTableAdapter>();
    mockAdapter.Setup(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0))).Returns(new BLOGDataTable());

    var blogBl = new BlogManagerBLL(mockAdapter.Object);
    var returnValue = blogBl.GetBlogsByBlogTitle("Thanks for reading my question");

    Assert.That(returnValue, Is.TypeOf(typeof(BLOGDataTable)));
}

我想我的建议是不要将它们放在一起,为每个创建一个测试。我想说你正在测试正确的东西。

If you're testing two things, you should write two tests.

[Test]
public void BlogTableAdapter_should_be_called_with_string_having_length_greater_than_1()
{
    var mockAdapter = new Mock<BLOGTableAdapter>();

    var blogBl = new BlogManagerBLL(mockAdapter.Object);
    blogBl.GetBlogsByBlogTitle("Thanks for reading my question");

    mockAdapter.Verify(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0)));
}

and

[Test]
public void BlogTableAdapter_should_return_a_BLOGDataTable_object()
{
    var mockAdapter = new Mock<BLOGTableAdapter>();
    mockAdapter.Setup(x => x.GetBlogsByTitle(It.Is<string>(s => s.Length > 0))).Returns(new BLOGDataTable());

    var blogBl = new BlogManagerBLL(mockAdapter.Object);
    var returnValue = blogBl.GetBlogsByBlogTitle("Thanks for reading my question");

    Assert.That(returnValue, Is.TypeOf(typeof(BLOGDataTable)));
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文