c# - Weird "Object reference not set to an instance of an object" error while using Moq -
c# - Weird "Object reference not set to an instance of an object" error while using Moq -
i trying run test "object reference not set instance of object". thoughts? using moq.
test method:
// arrange mock<icustomerrepository> customerrepo = new mock<icustomerrepository>(); client newcustomer = new customert() { id = 123456789, date = datetime.now }; customerrepo.setup(x => x.add()).returns(newcustomer); var controller = new customercontroller(customerrepo.object, new mock<iproductrepository>().object); // deed ihttpactionresult actionresult = controller.createcustomer();
createcustomer method:
client newcustomer = customerrepository.add(); //error occurs below homecoming created(request.requesturi + "/" + newcustomer.id.tostring(), new { customerid = newcustomer.id });
when set moq, need additionally configure httpcontext, otherwise request null. can set in function in controller phone call @ origin of test case, like:
private mock<controllercontext> getcontextbase() { var fakehttpcontext = new mock<httpcontextbase>(); var request = new mock<httprequestbase>(); var response = new mock<httpresponsebase>(); var session = new mockhttpsession(); var server = new mockserver(); var parms = new requestparams(); var uri = new uri("http://testurl/home/index"); var fakeidentity = new genericidentity("domain\\username"); var principal = new genericprincipal(fakeidentity, null); request.setup(t => t.params).returns(parms); request.setup(t => t.url).returns(uri); fakehttpcontext.setup(t => t.user).returns(principal); fakehttpcontext.setup(ctx => ctx.request).returns(request.object); fakehttpcontext.setup(ctx => ctx.response).returns(response.object); fakehttpcontext.setup(ctx => ctx.session).returns(session); fakehttpcontext.setup(ctx => ctx.server).returns(server); var controllercontext = new mock<controllercontext>(); controllercontext.setup(t => t.httpcontext).returns(fakehttpcontext.object); homecoming controllercontext; }
the supporting classes along lines of:
/// <summary> /// class allow simulation of sessionobject /// </summary> public class mockhttpsession : httpsessionstatebase { dictionary<string, object> m_sessionstorage = new dictionary<string, object>(); public override object this[string name] { { seek { homecoming m_sessionstorage[name]; } grab (exception e) { homecoming null; } } set { m_sessionstorage[name] = value; } } } public class requestparams : system.collections.specialized.namevaluecollection { dictionary<string, string> m_sessionstorage = new dictionary<string, string>(); public override void add(string name, string value) { m_sessionstorage.add(name, value); } public override string get(string name) { homecoming m_sessionstorage[name]; } } public class mockserver : httpserverutilitybase { public override string mappath(string path) { homecoming @"c:\yourcodepathtowherever\" + path; } }
lastly, in top of test method, add together call:
// arrange homecontroller controller = new homecontroller(); controller.controllercontext = getcontextbase().object;
that give request object work :)
[edit]
name spaces you'll need are:
using system.security.principal; using microsoft.visualstudio.testtools.unittesting; using moq;
c# unit-testing moq asp.net-web-api2
Comments
Post a Comment