NSubstitute检查已接来电没有工作

时间:2015-03-30 12:50:34

标签: c# unit-testing nunit nsubstitute

嘿,我们是新的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();
    }

}
}

1 个答案:

答案 0 :(得分:0)

您忘记在通知接收器中加入_track。它只是没有订阅事件,因此没有得到通知。要解决此问题,只需拨打IncomingTrack方法:

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

}