gpt4 book ai didi

c# - NSubstitute 检查收到的电话不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:43:19 26 4
gpt4 key购买 nike

大家好,我是 NSubstitute 框架的新手。我正在尝试测试我的一些类(class),但是当我使用 NSubstitute 检查收到的电话时,它说没有收到匹配的电话。

我正在尝试测试 Tick() 方法是否正在从轨道类接收 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();

}

关于c# - NSubstitute 检查收到的电话不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29346720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com