Saturday, 15 January 2011

c# - IHttpActionResult variable passing null in test method -



c# - IHttpActionResult variable passing null in test method -

i writing test method ihttpactionresult controller. actionresult not null , contains required info (customer.id = 986574123). in line 2 variable createdresult null. want homecoming appropriate info createdresult. using moq framework. don't know if matters. thoughts? if need more info actionresult please comment below. thx.

test method code:

var customerrepository = new mock<icustomerrepository>(); customerrepository.setup(x => x.add()).returns(new client { id = 986574123, date = datetime.now}); var controller = new customercontroller(customerrepository.object, new mock<iproductrepository>().object); var config = new httpconfiguration(); var request = new httprequestmessage(httpmethod.post, "http://localhost:38306/api/createcustomer"); var route = config.routes.maphttproute("defaultapi", "api/{controller}"); var routedata = new httproutedata(route, new httproutevaluedictionary { { "controller", "customers" } }); controller.controllercontext = new httpcontrollercontext(config, routedata, request); controller.request = request; controller.request.properties[httppropertykeys.httpconfigurationkey] = config; ihttpactionresult actionresult = controller.createcustomer(); // null occurs here var createdresult = actionresult createdatroutenegotiatedcontentresult<customer>;

createcustomer add together method:

[route("api/createcustomer")] [httppost] public ihttpactionresult createcustomer() { client newcustomer = customerrepository.add(); homecoming created(request.requesturi + "/" + newcustomer.id.tostring(), new { customerid = newcustomer.id }); }

actionresult data:

- location {http://localhost:38306/api/createcustomer/986574123} system.uri absolutepath "/api/createcustomer/986574123" string absoluteuri "http://localhost:38306/api/createcustomer/986574123" string authorization "localhost:38306" string dnssafehost "localhost" string fragment "" string host "localhost" string hostnametype dns system.urihostnametype isabsoluteuri true bool isdefaultport false bool isfile false bool isloopback true bool isunc false bool localpath "/api/createcustomer/986574123" string originalstring "http://localhost:38306/api/createcustomer/986574123" string pathandquery "/api/createcustomer/986574123" string port 38306 int query "" string scheme "http" string + segments {string[4]} string[] userescaped false bool userinfo "" string

the simplest alter create test pass alter line

return created(request.requesturi + "/" + newcustomer.id.tostring(), new { customerid = newcustomer.id });

to following

return created(request.requesturi + "/" + newcustomer.id.tostring(), newcustomer);

the problem that type parameter of createdatroutenegotiatedcontentresult not expect. seek cast result createdatroutenegotiatedcontentresult<customer> when in fact type createdatroutenegotiatedcontentresult<anonymoustype#1>, cast fails , returns null.

the reason apicontroller's create(string, t) method returns createdatroutenegotiatedcontentresult type parameter t type of content pass in, , you're passing in anonymous type.

you want utilize anonymous type homecoming fields model, want refer type outside of context declared (i.e. in unit test). isn't possible, see above link anonymous types (if must store query results or pass them outside method boundary, consider using ordinary named struct or class instead of anonymous type.)

so, if want homecoming fields, you'll need create specific view model purpose.

class customerdetails { public int customerid { get; set; } }

and in action method

return created(request.requesturi + "/" + newcustomer.id.tostring(), new customerdetails { customerid = newcustomer.id });

c# unit-testing moq asp.net-web-api2 actionmethod

No comments:

Post a Comment