嘿,我们是新的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?
}
}
}
答案 0 :(得分:0)
您正在尝试验证Tick
方法是否在HandleEvent
个对象上调用了LogEvent
和IEvent
。
为了做到这一点,您需要模拟IEvent
并以某种方式将该模拟传递给您的Tick
方法。
执行此操作的一种可能方法(在您当前的设计中)是让_eventDetection
模拟返回IEvent
模拟其DetectEvent
方法。您保留对此模拟的引用,并验证是否在其上调用了HandleEvent
和LogEvent
。