开发者

How do I Unit Test the Output of a View in MonoRail?

I've been trying to write some initial NUnit unit tests for MonoRail, having got some basics working already. However, while I've managed to check whether a Flash["message"] value has been set by a controller action, the BaseControllerTest class doesn't seem to store the output for a view at all, so whether I call RenderView or the action itself, nothing gets added to the Response.OutputContent data. I've also tried calling InPlaceRenderView to try to get it to write to a StringWriter, and the StringWriter also开发者_如何转开发 seems to get nothing back - the StringBuilder that returns is also empty. I'm creating a new controller instance, then calling PrepareController(controller,"","home","index");

So far it just seems like the BaseControllerTest is causing any output to get abandoned. Am I missing something? Should this work? I'm not 100% sure, because while I'm also running these unit tests in MonoDevelop on Linux, although MonoRails is working OK there.


While I haven't got an ideal method for testing Views, this is possibly less important when ViewComponents can be tested adequately. To test views within the site itself, I can use Selenium. While in theory that can be made part of an NUnit test suite, that didn't run successfully under MonoDevelop in my tests (failing to start the connection to Selenium RC consistently, despite the RC interactive session working fine). However, the Selenium tests can be run as a set from Firefox, which is not too bad - unit testing with NUnit, then Integration/System testing scripting using a Selenium suite, and that setup will work in a Linux/MonoDevelop setup.

As for testing the underlying elements, you can check for redirections and check the flash value set or the like, so that's all fine, and for testing ViewComponents the part-mocked rendering does return the rendered output in an accessible form, so they've proved much easier to test in NUnit (with a base test class of BaseViewComponentTest) as follows:

[Test]
public void TestMenuComponentRendersOK()
{
   var mc = new MenuComponent();
   PrepareViewComponent(mc);
   var dict = new System.Collections.Specialized.ListDictionary();
   dict.Add("data",getSampleMenuData());
   dict.Add("Name","testmenu");
   // other additional parameters
   mc.RenderComponent(mc,dict);
   Assert.IsTrue(this.Output.Contains("
  • "),"List items should have been added"); }
  • 0

    上一篇:

    下一篇:

    精彩评论

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

    最新问答

    问答排行榜