开发者

Am I misunderstanding something about Unit Tests?

开发者 https://www.devze.com 2023-02-17 14:22 出处:网络
Here is a test I wrote: [Test] public void Can_Generate_Links_To_Other_Pages() { //Arrange: We\'re going to extend the Html helper class.

Here is a test I wrote:

[Test]
public void Can_Generate_Links_To_Other_Pages()
{
    //Arrange: We're going to extend the Html helper class.
    //It doesn't matter if the variable we use is null            
    HtmlHelper html = null;

    PagingInfo pagingInfo = new PagingInfo(){
        CurrentPage = 2,
        TotalItems = 28,
        ItemsPerPage = 10
    };

    Func<int, String> pageUrl = i => "Page" + i;

    //Act: Here's how it should format the links.
    MvcHtmlString result = html.PageLinks(pagingInfo, pageUrl);

    //Assert:
    result.ToString().ShouldEqual(@"<a href=""Page1"">1</a><a class=""selected"" href=""Page2"">2</a开发者_JAVA技巧><a href=""Page3"">3</a>");
}

Please note how I'm hard coding the way the links should look like, which I think is a good idea for a test to do because that way if it doesn't look how I envision it, the test would fail.

I understand why this test works, after all my route in global.asax.cs is:

routes.MapRoute(
    null,
    "Page{page}",
    new { controller = "Products", action = "List" }
);

Great - but now if I change that route to:

routes.MapRoute(
    null,
    "page/{page}",
    new { controller = "Products", action = "List" }
);

The URL's change, as expected, right? But the test still passes. Since I hard coded the way the url should look like in the test, it's in my understanding that the test should fail, correct?

Am I missing something?

I've already Cleaned the project and rebuilt it using the new route but the test still passes.

I'm really curious about this, thanks!


Unit tests should eliminate dependencies. ASP.NET's routing is your dependency in this case - you shouldn't make it one. I'm not exactly sure what is happening in PageLinks, but if its just taking that model class and generating the HTML, then I say its doing its job properly if the test passes.


This is the kind of thing I typically don't bother to unit test. I find that manual testing, or something like a Coded UI Test is better at this sort of thing - send a user to determine whether a user interface is working.


are you missing Assert.That(...) wrap around your last line?

0

精彩评论

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