New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Received.InOrder executes return override on mocks and fails test #357
Comments
Full repro: using Xunit;
using NSubstitute;
using System.Collections.Generic;
using System.Linq;
public interface IThing {
bool SomeBool { get; set; }
}
public interface ISomeFactory {
IThing CreateThing();
}
public class WorkingObject {
private ISomeFactory Factory { get; }
public WorkingObject(ISomeFactory factory) {
Factory = factory;
}
public void DoStuff() {
var thing = Factory.CreateThing();
}
}
public class UnitTest1 {
[Fact]
public void DoStuff() {
var factory = Substitute.For<ISomeFactory>();
factory.CreateThing().Returns(x => {
var thing = Substitute.For<IThing>();
thing.SomeBool.Returns(true);
return thing;
});
var sut = new WorkingObject(factory);
sut.DoStuff();
// Passes:
factory.Received().CreateThing();
// Fails:
Received.InOrder(() => {
factory.CreateThing();
});
}
} |
Hi @trinith, Thanks for raising this. This seems related to the nested configuration for One workaround is to remove that nesting:
That may not be possible for your specific case, but it works for the case above so thought I should mention it. |
Hey, thanks for the response, and the confirmation! The workaround can certainly work in a lot of cases. In other ones, Received.InOrder wasn't as critical as I thought at the time and was able to change to using a single .Received().Method check. I'll make it work ;) |
@zvirja yes, it is. It seems we need to have a closer look at Roslyn analyzers. We can catch most of NSub syntax abuses. |
Hey :)
So lets say I have a factory that creates objects and I'm mocking that to make sure that it returns a mocked object with specific properties. In my [TestInitialize], I might do something like...
... Now lets say I have a method that I want to test on some other object that utilizes ISomeFactory to create things, and I want to make sure it makes the appropriate calls. I might write a test that contains the following....
This works fine and the test passes; however, if I instead write the test as follows...
... the test will fail with the following error...
The error text seems to suggest that what I'm doing on the factory returns method isn't advisable, but it seems quite reasonable to me and it works fine when we use the method that involves mock.Received().Method(). Additionally, if I put a breakpoint in the code for Returns, it is hit when using Received.InOrder(...), but not when using mock.Recevied().Method().
I dunno, hopefully that makes sense :)
The text was updated successfully, but these errors were encountered: