开发者

NSubstitute 'Do' event not firing

I have an issue with NSubstitute compiling using its inference typing. When I set it up with more than a return type in my .Do() statement and then try and use a counter to see how many times it is called, the counter isn't updated.

I am mocking a method that returns a string (it'开发者_StackOverflows basically an abstraction of File.ReadAllText()):

int fileReadCount =0;

IFileDataSource fs = Substitute.For<IFileDataSource>();

fs.When(x => x.ReadAllText(Arg.Any<string>())).Do(x =>
            {
                fileReadCount++;
                return "test";
            });

The alternate form mentioned in the documentation does seem to work either:

        fs.ReadAllText("test").ReturnsForAnyArgs(x =>
        {
            fileReadCount++;
            return "test";
        });

My counter never changes.

I am following the examples from http://nsubstitute.github.com/help/return-from-function/ so I don't understand what I am doing wrong - has the API changed?


This works for me in NSubstitute 1.1:

public interface IFileDataSource {
    string ReadAllText(string s);
}

[Test]
public void TestName() {
    int fileReadCount = 0;
    var fs = Substitute.For<IFileDataSource>();
    fs.ReadAllText("test").ReturnsForAnyArgs(x =>
       {
           fileReadCount++;
           return "test";
       });

    fs.ReadAllText("sdf");
    fs.ReadAllText("sdf");
    Assert.AreEqual(fileReadCount, 2);
}

Can you post some more code to show the problem?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜