0

I have unit test initialization:

private App fakeApp;
[TestInitialize]
public void initialize()
{
    Mock<App> mock = new Mock<App>();
    fakeApp = mock.Object;
    mock.Setup(m => m.CommLineInfo.Mode).Returns(RunMode.INSTALL);
}

where app is WPF main class:

public partial class App : Application
{
     public static CommandLine CommLineInfo { get; private set; }
}

where underlining object is just:

public class CommandLine
{
    public RunMode Mode
    {
        get { return Something.Mode; }
    }
}

and I'm getting errors like in topic. I know it could look like duplicate but I read other questions and didn't find any solution connected with mocking objects

Edit:

As Hantoun suggested I needded to make wrapper:

public virtual RunMode RunMode
{
    get
    {
        return App.CommLineInfo.Mode;
    }
}

and modify my code in initialization:

public void initialize()
{
    Mock<AppWrapper> mock = new Mock<AppWrapper>();
    fakeApp = mock.Object;
    mock.Setup(m => m.RunMode).Returns(RunMode.INSTALL);
}

Now its needed to modify method I'm testing but at least its working

szpic
  • 4,346
  • 15
  • 54
  • 85

1 Answers1

0

Try calling the CommLineInfo property statically:

mock.Setup(m => App.CommLineInfo.Mode).Returns(RunMode.INSTALL);
Sjeijoet
  • 741
  • 4
  • 20
  • `Invalid setup on a non-virtual (overridable in VB) member: m => App.CommLineInfo.Mode` – szpic Dec 03 '14 at 10:37
  • I've never worked with Moq before, but it seems the `Setup` does not support static methods or properties. It actually requires them to be virtual or abstract when mocking classes. Not sure if you've seen the following answer, but it might help you continue: http://stackoverflow.com/a/2416447/4065807 – Sjeijoet Dec 03 '14 at 10:53