Alex Gordon
Alex Gordon

Reputation: 60751

how to mock/inject a getter into a system under test?

Mark provides an elegant answer to a related question.

My class has a read only property:

public class myclass
{
    ...
    public virtual string Devicelocation => Message.Message.Items[0].ToString();
    ...
    public someMethod()
    {
        if(Devicelocation=="YourMom")
        {
            //dostuff
        }
        else
        {
            //dootherstuff
        }
    }
}

I would like to execute someMethod() with an assumption for what Devicelocation is equivalent to.

How do I mock or inject a value into Devicelocation?

Upvotes: 1

Views: 869

Answers (1)

Mohit
Mohit

Reputation: 11314

You can set up Devicelocation like this:

var stub = new Mock<myclass>();
stub.SetupGet(x => x.Devicelocation).Returns("YourMom");

stub.Object.Devicelocation will now return "YourMom".

Update:

stub.Object.someMethod();

Upvotes: 2

Related Questions