I have a text fixture which uses NUnit 3.4.1, NSubstitute 1.10.0 and NCrunch 2.23.0.2
There are at any point in time 2 faling tests in this fixture. Which test fail seems to vary every time I change something. Not all tests end up failing some time or another, but most do, and the problem is always an NSubstitute exception on a line like:
// _clock is initialized as _clock = Substitute.For<IClock>();
// the dates in the Returns statement change on every test
_clock.Now.Returns(new DateTime(2015, 1, 1));
I add NCrunch to this mix because all tests seem to pass with the Resharper 2016 test runner. Mostly.
The exception I invariably get is:
NSubstitute.Exceptions.UnexpectedArgumentMatcherException : Argument matchers
(Arg.Is, Arg.Any) should only be used in place of member arguments.
Do not use in a Returns() statement or anywhere else outside of a member call.
Which is clear enough, except that on mosts of the tests I don't use any Arg.Is
or Arg.Any
.
Here's the IClock
inerface in all it's glory. Now
is a getter-only property, but that shouldn't be a problem for NSubstitute, should it?
public interface IClock
{
DateTime Now { get; }
}
Sorry for the enormous amount of code coming up, but I don't want to assume it's due to one test or the other, so here goes:
[TestFixture]
public class AuctionTests : TestBase
{
#region Fields
AuctionService _auctionService;
IClock _clock;
Ride _ride;
IMailService _mailer;
#endregion
[SetUp]
public void Init()
{
_clock = Substitute.For<IClock>();
_mailer = Substitute.For<IMailService>();
_ride = new Ride
{
StartAuction = new DateTime(2016, 2, 12, 19, 0, 23),
PriceForCustomer = 20m,
InitialAuctionPrice = 15m,
HighestAuctionPrice = 19m
};
SetupData(_ride);
_auctionService = new AuctionService(RavenSession, _clock, _mailer);
}
[Test]
public void Auction_rejects_price_when_price_is_higher_then_HighestAuctionPrice()
{
const decimal price = 90m;
_clock.Now.Returns(new DateTime(2015, 1, 1));
var result = _auctionService.Accept(_ride.Id, price);
result.Should().BeFalse();
}
[Test]
public void Auction_rejects_price_when_inactive()
{
_clock.Now.Returns(new DateTime(2016, 2, 12, 20, 1, 23));
var result = _auctionService.Accept(_ride.Id, Arg.Any<decimal>());
result.Should().BeFalse();
}
[Test]
public void Auction_is_inactive_when_current_time_is_before_auction_startDate()
{
_clock.Now.Returns(new DateTime(2016, 2, 12, 18, 0, 23));
var result = _auctionService.Accept(_ride.Id, Arg.Any<decimal>());
Assert.IsFalse(result);
}
[Test]
public void Auction_is_active_when_current_time_is_exactly_auction_startDate()
{
_clock.Now.Returns(_ride.StartAuction);
var result = _auctionService.Accept(_ride.Id, _ride.InitialAuctionPrice);
result.Should().BeTrue();
}
[Test]
public void Action_price_is_valid_at_auction_start_time_if_equal_to_initial_price()
{
var price = _ride.InitialAuctionPrice;
_clock.Now.Returns(_ride.StartAuction);
var result = _auctionService.Accept(_ride.Id, price);
result.Should().BeTrue();
}
[Test]
public void Action_price_is_valid_at_auction_end_time_if_equal_to_highest_possible_price()
{
var price = _ride.HighestAuctionPrice;
_clock.Now.Returns(_ride.StartAuction.AddMinutes(60));
var result = _auctionService.Accept(_ride.Id, price);
result.Should().BeTrue();
}
[Test]
public void Action_price_is_invalid_if_not_within_time_parameters()
{
var price = 12m;
_clock.Now.Returns(_ride.StartAuction.AddMinutes(30));
var result = _auctionService.Accept(_ride.Id, price);
result.Should().BeFalse();
}
[Test]
[Ignore("Uitzoeken hoeveel seconde vertraging wenselijk is")]
public void Accept_takes_delay_in_requests_into_account()
{
var price = 17m;
_clock.Now.Returns(_ride.StartAuction.AddMinutes(30).AddSeconds(30));
var result = _auctionService.Accept(_ride.Id, price);
result.Should().BeTrue();
}
[Test]
public void Ride_is_saved_with_accepted_price()
{
var price = 17m;
var date = _ride.StartAuction.AddMinutes(30);
_clock.Now.Returns(date);
var result = _auctionService.Accept(_ride.Id, price);
Assert.IsTrue(result);
var dbRide = RavenSession.Load<Ride>(_ride.Id);
price.IsSameOrEqualTo(dbRide.AcceptedAuctionPrice);
}
[Test]
public void On_Start_Auction_InitialAuctionPrice_should_be_fifteen_percent_of_PriceForCustomer()
{
_ride.PriceForCustomer = 100;
_auctionService.StartAuction(_ride.Id);
Assert.AreEqual(85m, _ride.InitialAuctionPrice);
}
[Test]
public void On_Start_Auction_send_email_to_priorityPartners()
{
var priorityPartner = new Partner { Priority = true, Email = "[email protected]" };
SetupData(priorityPartner, new Partner { Priority = false });
_auctionService.StartAuction(_ride.Id);
_mailer.Received(1).SendAuctionEmail(Arg.Any<string>(), _ride);
}
}
Is there anything (obvious) that I'm missing? The quasi randomness of it all seems to point at code not being properly re-initialised for every test, but I completely fail to see how.
Any help is much appreciated.
These lines attempt to use an argument matcher with a non-substitute (_auctionService
):
var result = _auctionService.Accept(_ride.Id, Arg.Any<decimal>());
Argument matchers can only be used with substitutes, not with standard values created via new
.