Ivan Apungan
Ivan Apungan

Reputation: 534

How to mock a specific function

So I am setting up a function but I have 2 different parameters which I want to try setting up and has different return values, how do I do that?

mockStateFixture.MockCouchDbClient.Setup(x => x.AuthenticatedQuery(
   //It.IsAny<Func<HttpClient, Task<HttpResponseMessage>>>(), First Parameter Function# 1: GetProfileByUpn Function
   NamedHttpClients.COUCHDB,
   httpClient))
  .ReturnsAsync(httpResponseMessageForProfileRecordByUpn);


mockStateFixture.MockCouchDbClient.Setup(x => x.AuthenticatedQuery(
    //It.IsAny<Func<HttpClient, Task<HttpResponseMessage>>>(), First Parameter Function# 2: UpdateProfile Function
    NamedHttpClients.COUCHDB,
    httpClient))
   .ReturnsAsync(httpResponseMessageForCreatedReturnResult);

so the 1st one should have a different parameters from the 2nd one, they have different 1st parameters.

I am setting up a single function called AuthenticatedQuery but has different parameters, how do I setup the different parameters?

Upvotes: 2

Views: 134

Answers (2)

David McEleney
David McEleney

Reputation: 3813

Try FakeItEasy -

IMockedInterface mock = A.Fake<IMockedInterface>();

Object result1 = new {};
Object result2 = new {};

A.CallTo(() => mock.MethodName(1)).Returns(Object1);
A.CallTo(() => mock.MethodName(2)).Returns(Object2);

Upvotes: 1

Eric Olsson
Eric Olsson

Reputation: 4913

It looks like you may not be able to distinguish between the 2 calls you show setting up. I'm not sure how you'd distinguish between the 2 calls.

You could try using SetupSequence if you know the order of the 2 calls.

mockStateFixture.MockCouchDbClient.SetupSequence(x => x.AuthenticatedQuery(
    It.IsAny<Func<HttpClient, Task<HttpResponseMessage>>>(),
    NamedHttpClients.COUCHDB,
    httpClient))
  .ReturnsAsync(httpResponseMessageForProfileRecordByUpn)
  .ReturnsAsync(httpResponseMessageForCreatedReturnResult);

The responses will be in the order you specify them after the SetupSequence() call.

I haven't tried this, so I'm not exactly sure ReturnsAsync() can be chained like that.

Upvotes: 3

Related Questions