c# - Unit test for an application -
i displaying chart in mvc.
public class homecontroller : controller { mapcontext context = new mapcontext(); public actionresult index() { } }
how write test case check data. new mvc
you'll find hard write true unit test because you're instantiating concrete instance of mapcontext. ideally you'd use ioc/dependancy injection inject mapcontext , you'd want create interface can faked / mocked otherwise you're not testing controller you'retesting mapcontext no longer unit test intergration test..... lost yet?!
controller like:
public class homecontroller : controller { imapcontext _context; public homecontroller(imapcontext mapcontext) { _context = mapcontext; } public actionresult index() { var x = (from c in _context.area select c.name).toarray(); var y = (from c in _context.area select c.pin).toarray(); var bytes = new chart(width:500, height: 300) .addseries( charttype: "column", xvalue: x, yvalues: y) .getbytes("png"); return file(bytes, "image/png"); } }
then unit test (nunit + moq) :
[testfixture] public class homecontrollertest { mock<imapcontext> _mapcontext; [setup] public void setup() { _mapcontext = new mock<imapcontext>(); } [test] public void basictest() { httpconfiguration configuration = new httpconfiguration(); httprequestmessage request = new httprequestmessage(); var homecontroller = new homecontroller(_mapcontext.object); homecontroller.request = request; var result = homecontroller.index(); assert.isnotnull(result); assert.areequal(<somevalue>, result.someproperty); } }
obviously you'll need put in proper value in test against , change someproperty real property.
further reading find out more
- ioc/dependancy inject
- nunit
- moq
edit
here tutorials should started
https://msdn.microsoft.com/en-gb/library/dd410597(v=vs.100).aspx
https://msdn.microsoft.com/en-us/library/ff847525(v=vs.100).aspx
Comments
Post a Comment