1

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?

Community
  • 1
  • 1
Alex Gordon
  • 54,010
  • 276
  • 644
  • 1,024
  • It's not very clear what this DeviceLocation property does, you can just probably inject in in class constructor to some private field and make getter use this private field. – Red Mar 06 '16 at 13:22

1 Answers1

1

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();
Trikaldarshiii
  • 10,994
  • 16
  • 64
  • 93
  • how would i execute a concrete instance of someMethod(), assuming a specific value for devicelocation? – Alex Gordon Mar 06 '16 at 13:25
  • well when you are running a method on the object, i believe you are testing the moq framework itself and not the actual class that you've implemented and intend to test – Alex Gordon Mar 06 '16 at 17:21