Re: [RhinoMocks] Testing MVC ActionMethod results

80 views
Skip to first unread message

Patrick Steele

unread,
Oct 2, 2012, 12:00:15 PM10/2/12
to rhino...@googlegroups.com
You've got three Asserts in that test. Which one is failing?

---
Patrick Steele
http://weblogs.asp.net/psteele


On Tue, Oct 2, 2012 at 11:24 AM, D M <dorin....@gmail.com> wrote:
> I am trying to test an action method if returns expected values.
>
> here is my Action method & test code:
>
> [HttpPost]
> public JsonResult FindCustomer(SearchCriteria searchParams)
> {
> .....
>
> matchingCustomers = _databaseSearch.Find<SearchCriteria ,
> List<CompanyMatchItem>>(searchParams));
> matchingCustomers.AddRange(_webSearch.Find<SearchCriteria ,
> List<CompanyMatchItem>>(searchParams)));
>
> return Json(matchingCustomers);
> }
>
> [Test]
> public void
> test_credit_application_controller_FindCustomer_success()
> {
> var searchParams = new SearchCriteria();
> var companyMatchItems = new List<CompanyMatchItem>();
>
> var databaseSearch =
> MockRepository.GenerateMock<IDatabaseCompanySearch>();
> var webCompanySearch =
> MockRepository.GenerateMock<IWebCompanySearch>();
>
> databaseSearch.Expect(x => x.Find<SearchCriteria,
> List<CompanyMatchItem>>(searchParams)).Return(companyMatchItems).Repeat.AtLeastOnce();
> webCompanySearch.Expect(x => x.Find<SearchCriteria,
> List<CompanyMatchItem>>(searchParams)).Return(companyMatchItems).Repeat.AtLeastOnce();
>
> var myController = new MyController(databaseSearch, webSearch);
>
> var foundCustomers = myController.FindCustomer(searchParams);
>
> Assert.That(foundCustomers, Is.TypeOf<JsonResult>());
> Assert.That(foundCustomers, Is.Not.Null);
>
> var jsonResult = new JsonResult { Data = companyMatchItems };
>
> Assert.AreEqual(jsonResult.Data, foundCustomers.Data);
> }
>
>
>
> but the test fails with : Expected: <empty>
> But was: null
>
>
> I am not quite sure if it is correct to test for returned results?
>
> can anyone advise on this please?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Rhino.Mocks" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rhinomocks/-/6lkNm6Jk5tsJ.
> To post to this group, send email to rhino...@googlegroups.com.
> To unsubscribe from this group, send email to
> rhinomocks+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rhinomocks?hl=en.

D M

unread,
Oct 2, 2012, 12:14:31 PM10/2/12
to rhino...@googlegroups.com
Assert.AreEqual(jsonResult.Data, foundCustomers.Data); 
this one @_

bill richards

unread,
Oct 2, 2012, 1:38:39 PM10/2/12
to rhino...@googlegroups.com
For my two cents' worth ... and maybe a little off topic, but Patrick's reply above and this example in general pretty much show why we should only be testing one thing in each of our unit tests, i.e. only assert one thing for each test, this way there will be no ambiguity when pasting example code :)

Patrick Steele

unread,
Oct 2, 2012, 4:36:08 PM10/2/12
to rhino...@googlegroups.com
A few things I'll note:

First, In cases like this, I don't type check the result -- I just
cast it and let the cast (and the unit test) fail if the result isn't
the correct type.

var result = (JsonResult) myControler.xxx();
Assert.IsNotNull(result)

Second, you're doing an Assert.AreEqual on two objects. If you're
looking for them to be the exact same object, you should use
Assert.AreSame. But this test doesn't look right anyway since the
controller calls two methods and aggregates the results back into a
single variable and returns that. Your unit test should simply look
at "result.Data", cast it to what it should be and verify it for
accuracy.

For example, if a controller returned a JsonResult that contained a
List<int>, I would do something like this:

var result = (JsonResult) controller.Action();
var list = (List<int>) result.Data

// make sure list.Count is correct and contains the right data

The assumption is that your unit tests uses stubs and mocks so that
the list is small (3-4 items) and is easy to verify.

---
Patrick Steele
http://weblogs.asp.net/psteele


> https://groups.google.com/d/msg/rhinomocks/-/oPOQVKGGeUMJ.
Reply all
Reply to author
Forward
0 new messages