I am writing unittest for void method actually that method load the collection in ViewData["CityList"] method is
public void PopulateCityCombo() {
IEnumerable<Cities> c= service.GetCities();
ViewData["CityList"] = c.Select(e => new Cities{ ID = e.ID, Name = e.Name});
}
now i do not know how to unit test using Moq since controller method is void and not returning data, can any one tell i will achieve that.
I would actually ask what this method is actually doing? If it's an MVC controller, it must return an ActionResult (most likely a ViewResult) to be able to render anything. If it's a helper method than it should not be public and hence you shouldn't be unit
testing it (you'd be unit testing the public method(s) that use it, hence you'd still have coverage).
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
Actually this is loading data for dropdownlist(which is EditorTemplate) in mvc telerik grid.Telerik guys recomend this way to populate dropdownlist in grid.
My initial point still holds...if it's not returning an ActionResult, it's not an MVC Action and routing will never call it (i.e. it won't ever be used). Show us more of your code coz as it stands, your void method will never actually be called in production.
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
It's not that you can't test that. You can call the void method on the controller and check if the controller.ViewData has the correct items. The issue is that you should not be testing the internal implementation (the void method should really be private
as well). Testing private things couples your test tightly to your implementation. The test then - instead of being a safety net - become a burden in refactoring. This kind of defeats the purpose of the test.
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
braveboy76
Member
103 Points
96 Posts
using Moq - void return type method Unit test
Nov 17, 2011 12:23 PM|LINK
I am writing unittest for void method actually that method load the collection in ViewData["CityList"] method is
public void PopulateCityCombo() { IEnumerable<Cities> c= service.GetCities(); ViewData["CityList"] = c.Select(e => new Cities{ ID = e.ID, Name = e.Name}); }now i do not know how to unit test using Moq since controller method is void and not returning data, can any one tell i will achieve that.
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: using Moq - void return type method Unit test
Nov 17, 2011 03:50 PM|LINK
I would actually ask what this method is actually doing? If it's an MVC controller, it must return an ActionResult (most likely a ViewResult) to be able to render anything. If it's a helper method than it should not be public and hence you shouldn't be unit testing it (you'd be unit testing the public method(s) that use it, hence you'd still have coverage).
blog: www.heartysoft.com
twitter: @ashic
braveboy76
Member
103 Points
96 Posts
Re: using Moq - void return type method Unit test
Nov 17, 2011 04:14 PM|LINK
Actually this is loading data for dropdownlist(which is EditorTemplate) in mvc telerik grid.Telerik guys recomend this way to populate dropdownlist in grid.
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: using Moq - void return type method Unit test
Nov 17, 2011 04:17 PM|LINK
My initial point still holds...if it's not returning an ActionResult, it's not an MVC Action and routing will never call it (i.e. it won't ever be used). Show us more of your code coz as it stands, your void method will never actually be called in production.
blog: www.heartysoft.com
twitter: @ashic
braveboy76
Member
103 Points
96 Posts
Re: using Moq - void return type method Unit test
Nov 17, 2011 04:26 PM|LINK
When we load our page at that time this method call like
public ActionResult Detail()
{
PopulateCityCombo();}HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: using Moq - void return type method Unit test
Nov 17, 2011 04:34 PM|LINK
In that case, your test should look like this:
void TestMethod()
{
var service = setupMockServiceHereThatReturnsSomeCities;
var controller = new YourController(service.Object, other param...);
var result = controller.Detail as ViewResult; //I'm assuming you're returning some result...the snippet you've shown obviously isn't complete
//assert result.ViewData["CityList"] has the expected citites set up in the mock
}
And consider making your PopulateCityCombo method private. It has no reason to be public.
blog: www.heartysoft.com
twitter: @ashic
braveboy76
Member
103 Points
96 Posts
Re: using Moq - void return type method Unit test
Nov 17, 2011 04:36 PM|LINK
ok that means we can not test that directly, which i already did that but thanks for clarification.
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: using Moq - void return type method Unit test
Nov 17, 2011 09:37 PM|LINK
It's not that you can't test that. You can call the void method on the controller and check if the controller.ViewData has the correct items. The issue is that you should not be testing the internal implementation (the void method should really be private as well). Testing private things couples your test tightly to your implementation. The test then - instead of being a safety net - become a burden in refactoring. This kind of defeats the purpose of the test.
blog: www.heartysoft.com
twitter: @ashic