开发者

Asp.Net Mvc Unittesting a controller action

I have the following action that Im trying to unit test:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Default(ProductsViewModel model)
    {
        var locationId = model.LocationId;
        var locationText = model.LocationText;

        return locationId > 开发者_高级运维0 ? Summary(locationId, locationText, 1, "Date", true) : View();
    }

If the viewmodel is empty, then just a view is returned. But if it contains an id, then the summary action is called and does all the work (calling the db, building a new viewmodel etc).

But how can I assert in a unit test that it was called?


Presumably you already have tests for Summary that cover what it is supposed to do. In this case I would probably pick something that would differentiate the two, i.e., something that would be true only if Summary had been called and test for it. For example, in the case where Summary would have been called you can check for a non-null Model and perhaps that a Model property is set correctly. The only other alternative, that I can see, is to partially mock the controller and set up an expectation that the method is called.

Note, I'm not making the assumption that you're doing TDD, but if you were this would be moot. If you were doing TDD, then you'd already have a battery of tests for much of what Summary does (probably) and would have introduced using Summary as a result of refactoring. At that point, your existing tests -- and your tests for Summary -- would probably be sufficient and you wouldn't need any further tests. At least, you wouldn't need to duplicate any that the tests for Summary already cover.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜