0

Looking here, I'm led to believe that what I'm trying to do is possible. However, I seem to be getting a failing test, when I believe it should pass.

Here's my test code:

// Arrange
dbOperations = Substitute.For<IDbOperations>();
myClass = new MyClass(dbOperations);

string test = "test string";

// Act
myClass.MyMethod(arg1, arg2, test);

// Assert            
dbOperations.Received(2).TestMethod(Arg.Is<MyClass2>(a => a.MyString == test));

I've traced through MyMethod and it calls dbOperations twice with an instance of MyClass2 where the MyString property is set to "test string". Have I misread the docs here - is this kind of check even possibly with NSubtitute, and if so, what am I doing wrong?

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
  • 1
    The question in its current state is incomplete and therefore unclear. Read [ask] and then [edit] the question to provide a [mcve] that can be used to reproduce the problem, allowing a better understanding of what is being asked. – Nkosi Feb 16 '18 at 15:56

2 Answers2

1

Here is a sample that works as expected (using NSub 3.1.0):

using Xunit;
using NSubstitute;
using System;

public interface IDbOperations {
    void TestMethod(MyClass2 myClass2);
}

public class MyClass2 {
    public String MyString { get; set; }
}

public class MyClass {
    private IDbOperations Ops { get; }

    public MyClass(IDbOperations ops) { Ops = ops; }

    public void MyMethod(object arg1, object arg2, string test) {
        Ops.TestMethod(new MyClass2 { MyString = test });
        Ops.TestMethod(new MyClass2 { MyString = test });
    }
}

public class UnitTest1 {

    [Fact]
    public void StackOverflowQuestion() {
        // Arrange
        var arg1 = "1";
        var arg2 = "2";
        var dbOperations = Substitute.For<IDbOperations>();
        var myClass = new MyClass(dbOperations);

        string test = "test string";

        // Act
        myClass.MyMethod(arg1, arg2, test);

        // Assert            
        dbOperations.Received(2).TestMethod(Arg.Is<MyClass2>(a => a.MyString == test));
    }
}

I find it useful in cases like this to create a new, simplified version of what I'm trying to do and see if that works (like the one above), then spend some time looking at how my actual code is different from the simplified one. That should hopefully highlight where the problem is.

David Tchepak
  • 9,826
  • 2
  • 56
  • 68
  • Thanks. Strange thing: I tried your sample project, which worked; then I switched back to my project to try and ascertain the difference. Annoyingly, after no changes (other than the passage of time and a shutdown and restart of my machine), it worked. I'm therefore assuming that the test was actually passing, and VS was reporting a failure incorrectly. – Paul Michaels Feb 19 '18 at 22:04
  • @pm_2, argh those kinds of problems are so frustrating. Glad you got it sorted. :) – David Tchepak Feb 19 '18 at 23:16
-1

When comparing strings try to use the Equals method instead of ==. how-to-compare-strings

JJ Yong
  • 63
  • 9