C# NSSubstitute检查收到的呼叫不起作用

C# NSSubstitute检查收到的呼叫不起作用,c#,nunit,nsubstitute,C#,Nunit,Nsubstitute,嘿,伙计们,我是新加入NSubstitute框架的。我试图测试我的一些类,但是当我使用NSubstitute检查收到的调用时,它会说没有收到匹配的调用 我正在尝试测试Tick方法是否接收LogEvent和HandleEvent。。。来自事件类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ATM_Sy

嘿,伙计们,我是新加入NSubstitute框架的。我试图测试我的一些类,但是当我使用NSubstitute检查收到的调用时,它会说没有收到匹配的调用

我正在尝试测试Tick方法是否接收LogEvent和HandleEvent。。。来自事件类

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;//this list contains both Tracks and airpotrs
    private IRegion _region;
    private List<IEventDetection> _eventdetects;
    private List<IEvent> _events;



    public ATM(List<ITrack> airports, int region_size, List<IEventDetection> elist)
    //Sets airports, regionsize, and eventdetectors
    {
        _tracks = airports;
        _region = new Region.Region(region_size,region_size); 
        _events = new List<IEvent>();
        _eventdetects = elist;

    }

    public void Tick() //The tick function which is called each 250 ms
    {

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

        //check for events
        foreach (var detector in _eventdetects)
        {
            _events.AddRange(detector.DetectEvent(_tracks)); //this is simple: add the event list that the "detectevent" will 
                                                             //will return to the _events list
        }

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

            event1.LogEvent();
        }

    }

    public void Addairport(Airport AP)
    {
        _tracks.Add(AP);

    }

    public void IncomingTrack(ITrack track) //is called from main function when a new track is entering the region
    {
        //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 NSubstitute;
using NUnit.Framework;


namespace ATM_System.Tests.Unit
{
[TestFixture]
class ATMUnitTests
{
    private ATM _uut;
    private ITrack _track;
    private IEvent _event;
    private IEventDetection _eventDetection;

    private int _rsize;
    private List<ITrack> _tracks;
    private List<IEventDetection> _eDetections;


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

        _tracks = new List<ITrack>();
        _eDetections = new List<IEventDetection>();

        _uut = new ATM(_tracks, _rsize, _eDetections);

    }


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

    [Test]
    public void Tick_LogEvent_EventLogged()
    {
        //HOW?
    }

}
}

您正在尝试验证Tick方法是否在IEEvent对象上调用HandleEvent和LogEvent

为了做到这一点,您需要模拟IEvent,并以某种方式将该模拟传递给您的Tick方法

在您当前的设计中,一种可能的方法是让您的_eventdetectionmock在其DetectEvent方法中返回IEvent mock。您保留对此模拟的引用,并验证是否对其调用了HandleEvent和LogEvent。

的可能副本