Mock a function that returns a Expression Tree in a class

c# expression-trees mstest unit-testing

Question

Scenario:

I have a class with methods, in which one method returns a expression tree. How can I mock that method.

Code:

public Expression<Func<SpecFinderDataModel, bool>> BuildDynamicWhereClause(DataTableAjaxPostModel model)
    {
        var predicate = PredicateBuilder.New<SpecFinderDataModel>(true);
        if (_stringValidator.IsValid(model.search.value))
            predicate = _basicSearchService.DoSearch(model.search.value, predicate);
        var searchData = model.columns.Where(x => x.search.value != null);
        predicate = _advancedSearchService.DoSearch(model.isActive, searchData, predicate);
        return predicate;
    }

Here _advancedSearchService.DoSearch() this method returns a Expression<Func<SpecFinderDataModel, bool>>. I wrote a test which is given below

Test:

[TestMethod]
[TestCategory("BuildDynamicWhereClause")]
public void BuildDynamicWhereClauseTest()
{
    DataTableAjaxPostModel searchmodel = new DataTableAjaxPostModel()
    {
        columns = new List<Column>()
        {
            new Column() {
                data ="Status",
                orderable ="true",
                searchable ="true",
                search = new Search() {
                    regex = "false",
                    value ="TestStatus"
                }
            }
        },
        search = new Search()
    };

    IFinderBuildQueryFlow _finderBuildQueryFlow = new FinderBuildQueryFlow(
        _mockBasicSearchService.Object, _mockAdvancedSearchService.Object, _mockStringValidator.Object);

    var predicate = PredicateBuilder.New<SpecFinderDataModel>(true);
    _mockStringValidator.Setup(x => x.IsValid(searchmodel.search.value)).Returns(false);
    var searchData = searchmodel.columns.Where(x => x.search.value != null);
    _mockAdvancedSearchService.Setup(x => x.DoSearch(searchmodel.isActive, searchData, predicate).Compile()(model)).Returns(true);
    var test = _finderBuildQueryFlow.BuildDynamicWhereClause(searchmodel).Compile()(model);
}

Everything before _mockAdvancedSearchService.Setup(x => x.DoSearch(searchmodel.isActive, searchData, predicate).Compile()(model)).Returns(true); works fine. But I don't know how to mock advancedSearch.DoSearch() method.

Any advice would be really helpful. Thanks

Update #1:

This is the error when that line of code is executed

enter image description here

Update #2:

enter image description here

so here the predicate is returned as null.

and in the test I'm getting the error like this enter image description here

1
1
8/3/2018 7:58:12 AM

Popular Answer

If you want to mock the call to _advancedSearchService.DoSearch() you will have to set it up as follows:

Expression<Func<SpecFinderDataModel, bool>> query = model => true;
_mockAdvancedSearchService.Setup(x => x.DoSearch(searchmodel.isActive, It.IsAny<IEnumerable<Column>>(), It.IsAny<Expression<Func<SpecFinderDataModel, bool>>>())).Returns(query);

Given that the searchdata parameter is created in the method itself you will need to accept any value for it.

Given that the predicate parameter is created in the method itself you will need to accept any value for it.

1
8/3/2018 9:42:42 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow