KVN
KVN

Reputation: 982

Accessing runtime value for a property during FakeItEasy test

We're using FakeItEasy for our unit tests. I have a method1 that returns a value for one property, while updating it another property2 value during runtime.

What I need is to get the Updated value for property2 during my test. But it does not return the Updated value, it always returning the default/initial value. If I wrap this method1 into another method2 (that is only purpose to return the Updated value) - that I can get what I need but the code is ugly, because I'm creating new method for single testing purpose.

How can i pass the Updated value, while keeping my code clean?

Here is my code.

public class MyClass1
{

  public int Property3 = 0;

  public MyClass1()
  {
  }

  public virtual async Task<(Guid Property1, string Property2)> MyMethodOne (SendEmailMessageRequest request)
  {
     // Does something here
     property3++;

     return (request.Property1, request.Property2);
  }
}

Here is my test:

MyClass1 Sut => new MyClass1(); 

[Test]
        public void when_property3()
        {
            var fakeSendEmailMessageRequest = A.Fake<SendEmailMessageRequest>();

            (Guid Property1, string Property2) response;
            Action act = () => response = Sut.MyMethodOne(fakeSendEmailMessageRequest).ConfigureAwait(false).GetAwaiter().GetResult();

            act.Invoke();
            var rtr = Sup.Property3;
            rtr.Should().Be(1);
        }

Upvotes: 0

Views: 106

Answers (1)

jsami
jsami

Reputation: 356

Try to initialize your SUT before each test execution inside the Setup (I'm assuming that you are using NUnit here):

[SetUp]
public void Init()
{
    Sut = new MyClass1();
}

Upvotes: 1

Related Questions