c# – Nunit,NCrunch和NSubstitute – 使用UnexpectedArgumentMatcherException总是2次测试失败,它随机改变(准)

我有一个使用NUnit 3.4.1,NSubstitute 1.10.0和NCrunch 2.23.0.2的文本夹具

在任何时间点,该夹具中都有2次测试.每次更改内容时,哪个测试失败似乎都会有所不同.并非所有的测试最终都会失败一段时间,但大多数测试都会失败,并且问题始终是一条线上的NSubstitute异常:

// _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));

我将NCrunch添加到这个组合中,因为所有测试似乎都通过了Resharper 2016测试运行器.大多.

我总是得到的例外是:

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.

这很清楚,除了在大多数测试中我不使用任何Arg.Is或Arg.Any.

IClock接口

这就是IClock inerface的全部荣耀.现在是一个只有吸气剂的财产,但这应该不是NSubstitute的问题,不是吗?

public interface IClock
{
    DateTime Now { get; }
}

完整的夹具,即将到来

很抱歉出现了大量的代码,但我不想假设它是由于一个测试或另一个测试,所以这里是:

[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 = "some@email.com" };

        SetupData(priorityPartner, new Partner { Priority = false });

        _auctionService.StartAuction(_ride.Id);

        _mailer.Received(1).SendAuctionEmail(Arg.Any<string>(), _ride);
    }
}

有什么(显而易见的)我错过了吗?它的准随机性似乎都指向代码没有针对每个测试进行适当的重新初始化,但我完全没有看到如何.

任何帮助深表感谢.

解决方法:

这些行尝试使用带有非替代(_auctionService)的参数匹配器:

var result = _auctionService.Accept(_ride.Id, Arg.Any<decimal>());

参数匹配器只能与替代品一起使用,而不能与通过new创建的标准值一起使用.

上一篇:如何在C#单元测试中将测试Cookie添加到请求中


下一篇:c# – NSubstitute无法确定要使用的参数规范