unit testing - How to setup a controller's method using moq -
i have action method in controller below
public actionresult index() { var supplier = getsupplierforuser(); var model = supplierservice.getoutstandingitems(supplier); return view(model); }
i've setup supplier service method as
var supplierservice = new mock<isupplierservice>(); var supplier = new supplier { name = "some name",id = 100}; supplierservice.setup(s => s.getoutstandingitems(supplier)) .returns(outstandingsupplieriteminfo.object);
i don't know how can setup method supplier getsupplierforuser()
present in base controller return supplier object. moq setup above null supplier passed supplierservice.getoutstandingitems(supplier)
any ideas? thanks
maybe need:
supplierservice.setup(s => s.getoutstandingitems(it.isany<supplier>())) .returns(outstandingsupplieriteminfo.object);
the it.isany<>
stuff make setup
match incoming object (argument).
since use loose mock, if no setup
matches arguemnts in question, moq return null
. consider using mockbehavior.strict
have exception instead.
if not use it.isany<>
, moq have try see if supplier passed moq, "is equal to" supplier
used when made setup
. here can become important .equals(...)
semantics type (class
or struct
) supplier
has.
if not have relevant equals
semantics, still want restrict setup
particular situation, try instead:
supplierservice .setup(s => s.getoutstandingitems(it.is((supplier s) => s.name == "some name" && s.id == 100))) .returns(outstandingsupplieriteminfo.object);
Comments
Post a Comment