c# - Check calling for some method should belong to Unit Tests -
i trying follow test behaviour not method( that's doing). testing functionality in refresh items server.
it done in refreshitems() method. in method have calls other methods of other service can networkservice(for web request).
as implementation of refreshitems may change or may not calling specific method of networkservice.
so should unittests include test checking whether networkservice methods has been called or not ?
inject interface networkservice
. called inetworkservice
, iitemsservice
might surfice:
public interface iitemsservice { items getallitems(); //or whatever need }
then have 2 options:
mock , verify
inject mock interface , verify correct calls called, test implementation not behaviour.
var itemsservice = new mock<iitemsservice>(); //moq example var testobject = new classundertest(itemsservice.object); testobject.refreshitems(); itemsservice.verify(e => e.getallitems(), times.once); //verify calls moq
stub
create stub implementation of iitemsservice
returns fixed data. assert
new state of class under test.
var itemsservice = new stubitemsservice(); var testobject = new classundertest(itemsservice); testobject.refreshitems(); assert.areequal(1, testobject.items); //some assert on testobject
note can use moq stubbing too, rather create stub class:
var itemsservice = new mock<iitemsservice>(); itemsservice.setup(e => e.getitems()).returns(/*whatever*/); var testobject = new classundertest(itemsservice.object); testobject.refreshitems(); assert.areequal(1, testobject.items); //some assert on testobject
Comments
Post a Comment