Jacob Staggs
Jacob Staggs

Reputation: 9

Using Predicate in Mock It.Is

I am trying to mock my repository and use a predicate for the parameter but, when I call it, I am getting:

ienumerable<string> does not contain a definition for contains

Here is what I am trying to do:

_mockRepo.Setup(x => x.method(It.Is<IEnumerable<string>>(x => x.Contains(myObj)))).Returns(Task.FromResult(repoResponse));

The only error I am getting is where I call x.Contains(myObj) stating that ienumerable does not contain a definition for contains

Upvotes: 1

Views: 240

Answers (1)

Jason
Jason

Reputation: 1555

You are just missing a using statement to System.Linq:

using Moq;
using System.Collections.Generic;
using System.Linq; // <-- Add this
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var _mockRepo = new Mock<IRepo>();
            var myObj = "test";

            _mockRepo.Setup(x => x.method(It.Is<IEnumerable<string>>(x => x.Contains(myObj))))
                .Returns(Task.FromResult("Response"));

            var a = await _mockRepo.Object.method(new List<string> { "1", "2", "3" });
            var b = await _mockRepo.Object.method(new List<string> { "1", "2", "3", "test" });
        }
    }
    public interface IRepo
    {
        Task<string> method(IEnumerable<string> enumerable);
    }
}

enter image description here

Upvotes: 1

Related Questions