-2

i'm trying to mock a custom variable inside a class that's readonly and has a return inside the get.

public class BaseController
{
    public string Local
    {
        return GlobalVariable.Local
    }
}

[TestMethod]
public void TestMethod()
{
    var baseControllerMock = Substitute.For<BaseController>();
    baseControllerMock.Local.Returns("local");
}

My problem it's that even if i use ReturnsForAnyArgs when it goes inside the return I tired to look for the global variable and it breaks because

System.NullReferenceException: 'Object reference not set to an instance of an object.' since the GlobalVariable is null.

I also tryed to mock the global Variable

[TestMethod]
public void TestMethod()
{
    var baseControllerMock = Substitute.For<BaseController>();
    var globalVarMock = Substitute.For<GlobalVariable>();
    globalVarMock.Local.returns("local");
    baseControllerMock.Local.Returns("local");
}

but when it goes to the return it says that it is still null.

1 Answers1

0

Try making BaseController.Locale virtual.

NSubstitute can only work with overridable members of classes. If you add NSubstitute.Analyzers to your test project it will help detect cases that can not be substituted at compile time.


Edit from comments: This tests passes without error for me:

    public static class GlobalVariable { public static string Local = "hi"; }
    public class BaseController
    {
        public virtual string Local { get { return GlobalVariable.Local; } }
    }
    [TestMethod]
    public void TestMethod() {
        var baseControllerMock = Substitute.For<BaseController>();
        baseControllerMock.Local.Returns("local");
    }
David Tchepak
  • 9,826
  • 2
  • 56
  • 68
  • public class BaseController { public virtual string Local { return GlobalVariable.Local } } Like this? I've never used the virtual part – Ricardo Sanchez Santos Jun 18 '19 at 21:52
  • @RicardoSanchezSantos I'm not sure from your example if you are after a field or property? For a property having `public virtual string Local { get { return GlobalVariable.Local; } }` will pass your first test (although need to change `Locale` to `Local` in the test to get it to compile). – David Tchepak Jun 18 '19 at 22:47
  • the locale part was a typo, my bad, i already edit it so it says local like it should be, also y already add de virtual part `public virtual string Local { get { return GlobalVariable.Local; } }` and i ran the test again, but it still send me the NullReferance when it enters to the return – Ricardo Sanchez Santos Jun 18 '19 at 22:56
  • Hi @RicardoSanchezSantos, I have updated the answer with an example that passes on my machine. (Although I used `[Fact]` instead of `[TestMethod]` as I have XUnit rather than MSTest installed in my experimental project :) ) – David Tchepak Jun 18 '19 at 23:04
  • I also really recommend adding [NSubstitute.Analyzers](https://nsubstitute.github.io/help/nsubstitute-analysers/) to your test project. It will let you know at compile time if something can't be substituted (e.g. because it is non-virtual class member). – David Tchepak Jun 18 '19 at 23:11