i have a class extend from a base class,the base class in another dll.
public class MyMicroBlogCache : RelatedBase<THZUserInfo, MicroBlogCacheModel, int>
and in constructor,inject a ICache
public MyMicroBlogCache(ICache cache,....
in base class ,i use the ICache in a method(for example void A() )
and i write a unit test,mock the 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 with 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 with moq
and the strange thing is:
when i call the method write in base class(RelatedBase),the method(for example A()) call mock ICache,it not return the mock data.
if i override the method in child class(MyMicroBlogCache),and use the same code(override and copy the code from base class),it return the mock data.
if i override,and use base.A(...) ,it not return mock data.
so,when the code in base class,it wrong,and when code in child class,it right.
if i create a class implement ICache,it is ok
both NSubstitute and Moq are same.
i try this with vs2015,vs2013;.net 4.5
why it happens,and how can i 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 it call
var list = cache.PageRelated<TMainKey, TChild, TMainKey>("My", key, skip, take, out all, desc);
key=2217,skip=0,take=3,desc=true,