Domanda

Is it possible in Isolator to return a non-hardcoded value for a (read only) property?

It appears that this isn't possible without swapping out the faked object with a new instance of a concrete class with a property explicitly defined with the required behaviour (that would then presumably need a reference to the test method to access the required data)?

    public class MyClass
    {
        public int Number { get; private set; }
    }

    [Test]
    public void TestPropertyGetter_ReturningNonHardCodedValuesIsolator()
    {
        var fake = new MyClass();
        var x = 0;
        Isolate.WhenCalled(() => fake.Number).WillReturn(x);
        x++;

        Assert.AreEqual(1, fake.Number);
    }

Ideally, I'm looking for is a simple 1 liner equivalent to the MOQ syntax..

    fake.SetupGet(x => x.Number).Returns(() => x);
È stato utile?

Soluzione

My name is Nofar and I'm from Typemock's support team.

When you use the WillReturn API, you will get the same 'x' value it had while writing the willReturn phrase. So, changing it's value after this phrase is not relevant.

For this case, you can use the DoInstead API, like this:

    [TestMethod]
    public void TestPropertyGetter_ReturningNonHardCodedValuesIsolator()
    {
        var fake = new MyClass();
        var x = 0;
        Isolate.WhenCalled(() => fake.Number).DoInstead(y =>
                                                            {
                                                                return x;
                                                            });
        x++;

        Assert.AreEqual(1, fake.Number);
    }
}

May I ask what is it that you are trying to test?

Regards,

Nofar

Typemock Support

The Unit Testing Company

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top