c# - Should my MVC Controller tests return <empty> Objects from the database? Am I doing something wrong? -
i testing mvc controllers. relatively new particular method of testing. when mock controller , method properties, execute method, method returns <empty>
. either supposed or not connected database. added connection strings , data-sources app.config file , no luck. still returns <empty>
my app.config file (snippet)
<connectionstrings> <!-- <add name="dbcontexy" providername="system.data.sqlclient" connectionstring="persist security info=false;user id=user;password=password;initial catalog=db;data source=mysource" />--> <!-- inserted connection string below --> <add name="dbcontext" providername="system.data.sqlclient" connectionstring="user id=user;password=hello;data source=examplesource;database=mydb" /> <!--<add name="dbcontext" providername="system.data.sqlclient" connectionstring="persist security info=false;user id=user;password=password;initial catalog=sb;data source=.\mysource" />--> </connectionstrings>
my test - returns <empty>
when assert.aresame called instead of .areequal
[test] public void getcontacts_returncontacts() { //arrange var mockcontactmanager = a.fake<icontactmanager>(); var mockcontext = a.fake<callerinfo>(); var mockcallerinfomanager = a.fake<icallerinfomanager>(); var mocksiterepository = a.fake<isiterepository>(); var mockcontactcontroller = a.fake<contactcontroller>(); mockcontext.sitelocationcode = "us1"; const int mockcontactid = 168; mockcontext.contactid = mockcontactid; list<contact> expected = new list<contact> { }; // expect a.callto(() => mockcontactmanager.getcontacts(mockcontext.sitelocationcode)).returns(expected); using (mockcontactcontroller = new contactcontroller(mockcontactmanager, mockcallerinfomanager, mocksiterepository)) { //act list<contact> returnedcontacts = mockcontactcontroller.getcontacts(); //assert assert.areequal(expected, returnedcontacts); } }
my controller
[httppost] [actionname("retrievecontacts")] public list<contact> getcontacts([frombody]string query) { var context = getcontext(); return _contactmanager.getcontacts(context.sitelocationcode, query); }
not sure trying test. if mocked interface not use database connection strings specified. fake class imitates interface without doing real work can pass functions expect type of object. if want use real database don't use mocks.
Comments
Post a Comment