0

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);
        }
KVN
  • 863
  • 1
  • 17
  • 35
  • 1
    It would help if you would post your actual code. The code you posted doesn't even compile, so it's hard to know what the problem is exactly. Anyway, from what I can guess from seeing your code, I would expect the value of `property3` (which is a field, not a property) to be 1, not 5. – Thomas Levesque Apr 21 '20 at 07:44
  • Thanks @ThomasLevesque, The actual code is quite big and complicated (will bring more methods in). But what I have found - is that if I move `MyClass1 Sut => new MyClass1();` inside the Test method - then it works correctly. The `rtr` will get the Updated value. Although not 100% sure why. – KVN Apr 21 '20 at 15:35
  • And I have updated the Expected to be '1' now too. Just to make it less confusing. – KVN Apr 21 '20 at 15:36

1 Answers1

1

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();
}
jsami
  • 356
  • 1
  • 8