-
-
Notifications
You must be signed in to change notification settings - Fork 802
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
SetupSequence does not work when returning null first #1003
Comments
I cannot reproduce what you're describing. Can you please provide minimal but complete repro code? (Also, please test with the latest version of Moq, since that is the only one we're working on.) Here's a working counterexample: public interface ICollection<T>
{
T Get(int id);
}
public class Company
{
public Company(int id)
{
this.Id = id;
}
public int Id { get; }
}
public interface IEvilLord
{
ICollection<Company> Companies { get; }
}
[Fact]
public void Test()
{
var nullCompany = (Company)null;
var emptyCompany = new Company(1);
var mock = new Mock<IEvilLord>();
mock.SetupSequence(m => m.Companies.Get(1))
.Returns(nullCompany)
.Returns(emptyCompany);
Assert.Null(mock.Object.Companies.Get(1));
Assert.Same(emptyCompany, mock.Object.Companies.Get(1));
} |
@stakx Thanks for the prompt reply. You were right, even with the version I'm working with, it worked as expected. I got fixated on a specific chunk of code and missed an additional call being made to the function so it looks like it wen from null, object, null thus making me think it was return null, null when starting with null. I would have thought it would have thrown an error of some sort if called too many times but it either is returning null or cycling back up through the sequence. I'm not sure but at least it is not a bug as I originally thought and I can continue with my work. Apologies & thanks. Thierry |
Not a bug so closing |
Might have spoken too quickly but will keep investigating. |
At least for a loose mock, that's the expected behavior. For calls that either don't match a setup, or match a setup that has no return value configured, a default value (as per your |
Hi,
I think I just spotted a bug when using the SetupSequence when trying to return is null first.
For example:
When calling Companies.Get(1), it returns null and when calling it the second time, it also returns null but if you swap the values around for the return
It will work as expected and return the empty company and then the null value.
I've tried it using a single .Return with an Queue, same thing. tried to call a function instead for each .Return definition just to try different things, but the behaviour is the same for everything I've tried.
This is happening with version: 4.10.0.0 using .NET 2017.
Any workaround would be great.
Thanks
The text was updated successfully, but these errors were encountered: