0

I'm not expert with mocking, but I know mocks advantages. I face with a problem where I have an interface you can see it below. I want to Setup this interface get property in the right way.

public interface IInterface
{
    string this[string key] { get; }
}

I tried something like this:

Mock<IInterface> mockInterface = new Mock<IInterface();
mockInterface.SetupGet(mockInterface.Object).Returns("Test"); 

But I get the following error:

Error CS0411 The type arguments for method 'Mock.SetupGet(Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Can you give me some advice or solution for this problem?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
totesz09
  • 164
  • 1
  • 11
  • Have a look at `Task.FromResult("Test")` – Hans Kilian Feb 08 '19 at 07:59
  • I think [this question](https://stackoverflow.com/questions/340827/how-to-moq-an-indexed-property) should answer your question. That kind of property is called an [`Indexer`](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/). – ProgrammingLlama Feb 08 '19 at 08:08
  • 2
    @John I'm coming with a beer to you thanks! :beer: – totesz09 Feb 08 '19 at 08:12

0 Answers0