Matt1990
Matt1990

Reputation: 49

NSubstitute object can't use Returns() extension method

I'm working on VB project in our company. It's quite old, but under active development. Recently we introduced Unit Tests, new features must be covered, we're also picking some old features if we have spare time. We're using NUnit 3.9 and NSubstitute 2.0.3, test project targets .NET Framework 3.5.

Update: As Lasse Vågsæther Karlsen suggested, I prepared MCV for this question, here's the code which fails:

Dim dataReaderSubstitute As IDataReader = Substitute.For(Of IDataReader)()
dataReaderSubstitute.GetValue(0).Returns(email) 'test fails here

To make sure that problem still exists in newest version of NSubstitute and can be quickly reproduced, I created separate project in .net 4.6.2 with NUnit 3.9.0 and NSubstitute 3.1.0

Imports NUnit.Framework
Imports NSubstitute

<TestFixture> Public Class SubstituteProblemTest

Private Shared ReadOnly Iterator Property PersonTestData() As IEnumerable(Of TestCaseData)
    Get
        Yield New TestCaseData("[email protected]")
    End Get
End Property

<Test, TestCaseSource("PersonTestData")> Public Sub SubstituteProblem(ByVal email As String)
    Dim dataReaderSubstitute As IDataReader = Substitute.For(Of IDataReader)()
    dataReaderSubstitute.GetValue(0).Returns(email) 'test fails here
End Sub

End Class

The problem is that I get an error in second line of SubstituteProblem method:

Test Name: SubstituteProblem("[email protected]")
Test FullName: NSubstituteTest.SubstituteProblemTest.SubstituteProblem("[email protected]")
Test Source: c:\git\NSubstituteTest\NSubstituteTest\SubstituteProblemTest.vb : line 12
Test Outcome: Failed
Test Duration: 0:00:00.368

Result Message: System.NullReferenceException : Object variable or With block variable not set.
Result StackTrace:
at Microsoft.VisualBasic.CompilerServices.Symbols.Container..ctor(Object Instance)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
at NSubstituteTest.SubstituteProblemTest.SubstituteProblem(String forename) in c:\git\NSubstituteTest\NSubstituteTest\SubstituteProblemTest.vb:line 14

I'm not sure what I'm doing wrong, I was following example from NSubstitute website https://nsubstitute.github.io/help/set-return-value/. Why does Returns method couldn't change object created by NSubstitute?

Upvotes: 1

Views: 2840

Answers (1)

Matt1990
Matt1990

Reputation: 49

Looks like that was my mistake. I was not aware of something called "Late Binding", which is a feature of Visual Basic language. When I tried to use Returns method on instance of type Object, compiler tried to execute method as if it was part of class, not extension. So I had to execute extension method explicitly:

Dim dataReaderSubstitute As IDataReader = Substitute.For(Of IDataReader)()
SubstituteExtensions.Returns(dataReaderSubstitute.GetValue(0), email)

Upvotes: 1

Related Questions