multiple parameters call back in Moq

2020-06-02 17:10发布

Can someone please look at the code below and see what's wrong?

[TestInitialize]
public void SetupMockRepository()
{
    var memberId = "34345235435354545345";
    var title = "test";
    var url = "dafdsfdsfdsfdsafd";

    _mockPropertySearchRepository = new Mock<IPropertySearchRepository>(MockBehavior.Strict);
    _mockPropertySearchRepository
        .Setup(p => p.SaveSearchURL(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
            .Callback<string,string,string>((id,t,u) =>  )
            .Returns(new SavedSearchReturnResult() );

}

Thanks

标签: callback moq
2条回答
爷的心禁止访问
2楼-- · 2020-06-02 17:49

For each parameter that the method takes, pass a type parameter to the Callback method.

someMock
    .Protected()
    .Setup("SomeMethod", ItExpr.IsAny<string>(), ItExpr.IsAny<string>())
    .Callback<string, string>((x, y) => {});

The above works both with Protected and normal callbacks.

查看更多
The star\"
3楼-- · 2020-06-02 17:53

I managed to solve the problem myself as below

[TestInitialize]
public void SetupMockRepository()
{
    var memberId = "34345235435354545345";
    var title = "test";
    var url = "dafdsfdsfdsfdsafd";


    _mockPropertySearchRepository = new Mock<IPropertySearchRepository>(MockBehavior.Strict);
    _mockPropertySearchRepository
        .Setup(p => p.SaveSearchURL(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
        .Callback<string,string,string>(
            (id, t, u) =>
            {
                memberId = id;
                title = t;
                url = u;
            })
        .Returns(new SavedSearchReturnResult());
}
查看更多
登录 后发表回答