Search code examples
asp.net-mvcunit-testingwatinui-testing

UI Testing MVC with WatiN and injecting parameter to controller


First off, I may be barking up the wrong tree with this, so please correct me if I am wrong.

That said, I am trying to write sme UI unit tests that use WatIn. I am only working on a subset of the UI at present.

I have a controller called Product along with its associated views. the Controller takes in a ProductRepository via the constructor.

What I would lie to do is to test that different product categories are rendered correctly, in different tests, so I need to pass in some kind of stubbed or mocked repository into my code.

My test is set up along the lines of;

using (var ie = new IE("http://localhost:2904/Product"))
{
     ...
}

So how do I pass in a repository to my controller that will provide the appropriate product? Once I have called the using statement, my system has launched the browser, so that is too late. But until the browser is launched, then the controller doesnt exist, so I can't inject in the appropriate repository.

Or have I fundamentally got this wrong, and I can only use WatiN against my database, and I need to program it to select a member of each type of product from the db, adn test that way?


Solution

  • I think, as you suggesting, you pick the wrong end of the problem. If I understand correctly, you don't need UI test and Watin but rather integration tests. I would suggest to create instance of controller, inject repositories as needed and mock other dependencies. Than you can switch repositories and so.

    In UI tests you test your whole application from the top of UI down to database so it's hard to change behavior or mock some parts. Do you really need to test your functionality with UI? Wouldn't be better to test it only in controller or it's not option for your case?

    Seems ok or I misunderstand you completely?