visual studio - unit test strange things -


i have class extend base class,the base class in dll.

public class mymicroblogcache : relatedbase<thzuserinfo, microblogcachemodel, int> 

and in constructor,inject icache

public mymicroblogcache(icache cache,.... 

in base class ,i use icache in method(for example void a() )

and write unit test,mock icache

var cache = substitute.for<icache>(); cache.pagerelated<thzuserinfo, microblogcachemodel, int>("my", 2217, 0, 3, out all, true) .returns(     x =>     {         var list = new list<microblogcachemodel>();         list.add(new microblogcachemodel { id = 1, userid = 2217 });         list.add(new microblogcachemodel { id = 1, userid = 2217 });         list.add(new microblogcachemodel { id = 1, userid = 2217 });         return list;     }); 

mock nsubstitute

var cache1 = new mock<icache>(); cache1.setup(ca => ca.pagerelated<thzuserinfo, microblogcachemodel, int>("my", 2217, 0, 3, out all, true)) .returns( () => {     var list = new list<microblogcachemodel>();     list.add(new microblogcachemodel { id = 1, userid = 2217 });     list.add(new microblogcachemodel { id = 1, userid = 2217 });     list.add(new microblogcachemodel { id = 1, userid = 2217 });     return list; }); 

or mock moq

and strange thing is:
when call method write in base class(relatedbase),the method(for example a()) call mock icache,it not return mock data.
if override method in child class(mymicroblogcache),and use same code(override , copy code base class),it return mock data.
if override,and use base.a(...) ,it not return mock data.
so,when code in base class,it wrong,and when code in child class,it right.
if create class implement icache,it ok
both nsubstitute , moq same.
try vs2015,vs2013;.net 4.5
why happens,and how can fix it

update:

        var cache1 = new mock<icache>(mockbehavior.strict);         cache1.setup(ca => ca.pagerelated<thzuserinfo, microblogcachemodel, int>("my", 2217, 0, 3, out all, true))             .returns(             () =>             {                 var list = new list<microblogcachemodel>();                 list.add(new microblogcachemodel { id = 1, userid = 2217 });                 list.add(new microblogcachemodel { id = 1, userid = 2217 });                 list.add(new microblogcachemodel { id = 1, userid = 2217 });                 return list;             }); 

then test method call

var r = my.getrelatedpage(2217, 0, 3, out all, true); 

in base class call

var list = cache.pagerelated<tmainkey, tchild, tmainkey>("my", key, skip, take, out all, desc); 

key=2217,skip=0,take=3,desc=true,

you have generic base class:

relatedbase<t1, t2, t3> 

you have inherited class without generic parameters, specifies type each t1, t2, t3.

with genericity, relatedbase<thzuserinfo, microblogcachemodel, int> not same type relatedbase<t1, t2, t3>

now: in set-up, explicitly setup ca.pagerelated<thzuserinfo, microblogcachemodel, int>

if, call method on base class (the generic one) different argument types, not same call 1 setup, , therefore, return default value method.

try instantiating mock new mock<icache>(mockbehavior.mockstrict) , should have mockexception confirms that.

then, think of type arguments proper arguments : if don't match, setup doesn't apply.

hope helps :)


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -