大家好,我是NSubstitute框架。我正在尝试测试一些类,但是当我使用NSubstitute检查收到的呼叫时,它说没有收到匹配的呼叫。

我正在尝试测试方法Tick()是否从track类接收update()。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ATM_System.Event;
using ATM_System.EventDetection;
using ATM_System.Region;
using ATM_System.Track;

namespace ATM_System
 {
public class ATM
{
    private List<ITrack> _tracks;
    private IRegion _region;
    private List<IEventDetection> _eventdetects;
    private List<IEvent> _events;

    public ATM()
    {
        _tracks = new List<ITrack>();
        _region = new Region.Region(100000,100000); //could be changed by user
        _events = new List<IEvent>();
        _eventdetects = new List<IEventDetection>();


    }



    public void Tick()
    {

        // update track positions
        foreach (var track1 in _tracks)
        {
            track1.update();
        }

        //check for events
        foreach (var detector in _eventdetects)
        {
            _events.AddRange(detector.DetectEvent(_tracks));
        }

        //handle events and output
        foreach (var event1 in _events)
        {
            event1.HandleEvent();
            event1.LogEvent();
        }

    }

    public void IncomingTrack(ITrack track)
    {
        //add incoming track
        _tracks.Add(track);
    }
}
}


测试文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ATM_System.Event;
using ATM_System.EventDetection;
using ATM_System.Track;
using NUnit.Framework;
using NSubstitute;

namespace ATM_System.Tests.Unit
{
[TestFixture]
class ATMUnitTests
{

    private ATM _uut;
    private ITrack _track;
    private IEvent _event;
    private IEventDetection _eventDetection;

    [SetUp]
    public void Setup()
    {
        _track = Substitute.For<ITrack>();
        _event = Substitute.For<IEvent>();
        _eventDetection = Substitute.For<IEventDetection>();

        _uut = new ATM();
    }

    [Test]
    public void Tick_UpdateTracks_TracksUpdated()
    {
        _uut.Tick();
        _track.Received().update();
    }

}
}

最佳答案

您忘记在通知接收方中包含_track。它只是没有订阅事件,因此没有通知。要解决此问题,只需调用您的IncomingTrack方法:

[Test]
public void Tick_UpdateTracks_TracksUpdated()
{
    _uut.IncomingTrack(_track);
    _uut.Tick();
    _track.Received().update();

}

10-07 14:04