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 On Tue, Oct 2, 2012 at 12:14 PM, D M <[email protected]> wrote: > Assert.AreEqual(jsonResult.Data, foundCustomers.Data); > this one @_ > > > On Tuesday, October 2, 2012 5:00:16 PM UTC+1, Patrick Steele wrote: >> >> 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 <[email protected]> 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 [email protected]. >> > To unsubscribe from this group, send email to >> > [email protected]. >> > For more options, visit this group at >> > http://groups.google.com/group/rhinomocks?hl=en. > > -- > 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/-/oPOQVKGGeUMJ. > > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/rhinomocks?hl=en. -- You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rhinomocks?hl=en.
