开发者

using Moq and NUnit , what is write approach / better syntax?

开发者 https://www.devze.com 2022-12-16 16:37 出处:网络
I am trying to test this behavior --BLOGTableAdapter.GetBlogsByTitle(string title) is called and for on开发者_Go百科ce only

I am trying to test this behavior

-- BLOGTableAdapter.GetBlogsByTitle(string title) is called and for on开发者_Go百科ce 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消