开发者

Calling original method with Moq

开发者 https://www.devze.com 2023-01-03 19:45 出处:网络
I have a ProductRepository with 2 methods, GetAllProducts and GetProductByType, and I want to test the logic at GetProductByType. Internally, GetProductByType makes a call to GetAllProducts and then f

I have a ProductRepository with 2 methods, GetAllProducts and GetProductByType, and I want to test the logic at GetProductByType. Internally, GetProductByType makes a call to GetAllProducts and then filters the correct ones.

public virtual IEnumerable<Product> GetAllProducts()
{
    //returns all products in memory, db etc
}

public virtual IEnumerable<Product> GetProductsByType(string type)
{
    return (from p in GetAllProducts() where p.Type == type select p).ToList();
}

So in my test I'd like to mock the call to GetAllProducts, so it returns a list of products defined at my test, and then call the original GetProductsByType, which will consume the mocked GetAllProducts.

I'm trying something 开发者_运维问答like the code below but the original GetProductByType is not executed, it is mocked-out as well. In TypeMock I have a CallOriginal method that fixes this, but I can't figure it out with Moq. Any ideas?

var mock = new Mock<ProductRepository>();
mock.Setup(r => r.GetAllProducts()).Returns(new List<Product>() {p1, p2, p3});
var result = mock.Object.GetProductsByType("Type1");
Assert.AreEqual(2, result.Count());


Set CallBase to true on your mock. This will call the original virtual methods or properties if they exist, and haven't been set up to return a canned value.

var mock = new Mock<ProductRepository>() { CallBase = true };


today I found, in moq now can use this method:

 mockObj.Setup(obj => obj.FunctionA()).CallBase();
0

精彩评论

暂无评论...
验证码 换一张
取 消