0

Hey guys im new with the NSubstitute framework. I'm trying to test some of my classes, but when i use NSubstitute to check received calls it says received no matching calls.

I'm trying to test if the method Tick() is receiving LogEvent() and HandleEvent(...) from event class.

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);
    }
}
}

TEST FILE

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?
    }

}
}
Loc Dai Le
  • 1,661
  • 4
  • 35
  • 70
  • possible duplicate of [NSubstitute Checking received calls don't work](http://stackoverflow.com/questions/29346720/nsubstitute-checking-received-calls-dont-work) – forsvarir May 20 '15 at 21:10

1 Answers1

0

You are trying to verify that your Tick method calls HandleEvent and LogEvent on IEvent objects.

In order to do this, you will need to mock IEvent and somehow pass that mock to your Tick method.

One possible way to do this (in your current design) is to have your _eventDetection mock return said IEvent mock in its DetectEvent method. You keep a reference to this mock and verify that HandleEvent and LogEvent are called on it.

transporter_room_3
  • 2,583
  • 4
  • 33
  • 51