繁体   English   中英

状态设计模式修改上下文类集合-如何进行单元测试。

[英]State design pattern modifying context class collection - How to unit test.

我目前正在使用nSubstitute和nUnit进行一些单元测试。 我是一个新手,涉及到这些东西,所以任何帮助将不胜感激。 请看下面的代码:

public class Event
{
    private ISet<Host> hostSet; 
    private IEventState eventState; 

    public Event()
    {
        hostSet = new HashSet<Host>(); 
        eventState = new StartedEvent(); 
    }

    public virtual string Name { get; protected internal set; }

    public virtual bool CanSetName()
    {
        return eventState.CanSetName(); 
    }

    public virtual void SetName(string name)
    {
        eventState.SetName(this, name);
    }

    internal virtual ISet<Host> HostSet
    {
        get
        {
            return hostSet; 
        }
    }

    public virtual ReadOnlyCollection<Host> HostCollection
    {
        get
        {
            return new ReadOnlyCollection<Host>(hostSet.ToList()); 
        }
    }

    public virtual bool CanAddHost()
    {
        return eventState.CanAddHost(); 
    }

    public virtual void AddHost(string firstName, string lastName)
    {
        eventState.AddHost(this, firstName, lastName);
    }

    internal virtual void ChangeState(IEventState eventState)
    {
        this.eventState = eventState; 
    }

}

public class Host
{
    private readonly string firstName; 
    private readonly string lastName;

    public Host(string firstName, string lastname)
    {
        this.firstName = firstName; 
        this.lastName = lastname; 
    }

    public string FirstName
    {
        get
        {
            return firstName; 
        }
    }

    public string LastName
    {
        get
        {
            return lastName; 
        }
    }
}

public interface IEventState
{
    bool CanSetName(); 
    void SetName(Event evnt, string Name); 
    bool CanAddHost(); 
    void AddHost(Event evnt, string firstName, string lastName); 
}

internal class StartedEvent : IEventState
{

    public bool CanSetName()
    {
        return true; 
    }

    public void SetName(Event evnt, string name)
    {
        evnt.Name = name; 
    }

    public bool CanAddHost()
    {
        return true; 
    }

    public void AddHost(Event evnt, string firstName, string lastName)
    {
        evnt.HostSet.Add(new Host(firstName, lastname)); 
    }
}

[TestFixture]
public class StartedEventState
{
    [Test]
    public void CanSetName_Always_ReturnsTrue() 
    {
        IEventState eventState = new StartedEvent();
        bool result = eventState.CanSetName();
        Assert.True(result);
    }

    [Test]
    public void SetName_WhenCalled_ChangesTheEventName() 
    {
        Event evnt = Substitute.For<Event>();
        IEventState eventState = new StartedEvent();
        eventState.SetName(evnt, "Test Event");
        evnt.Received().Name = "Test Event";
    }

    [Test]
    public void CanAddHost_Always_ReturnsTrue() 
    {
        IEventState eventState = new StartedEvent();

        bool result = eventState.CanAddHost();

        Assert.True(result);
    }

    [Test]
    public void AddHost_WhenCalled_AddsTheHostToTheEvent()
    {
        //No idea how to test this? 
    }
}

[TestFixture]
public class EventTests
{
    [Test]
    public void CanSetName_WhenCalled_DelegatesToItsCurrentState()
    {
        Event evnt = new Event();
        var eventState = Substitute.For<IEventState>();
        evnt.ChangeState(eventState);
        evnt.CanSetName();
        evnt.Received().CanSetName();
    }
}

当前编写的测试全部通过。 但是我不知道如何执行测试以确保StartedEvent.AddHost方法正确调用上下文类并将其添加到集合中。 我尝试了一些不同的尝试,但失败了。

1)有人可以向我指出正确的方向,以便我测试该方法。 2)如果有人可以确认我现有的测试还可以,那就太好了。

要测试StartedEvent.AddHost

  1. 建立Event

    事件event = new Event();

  2. 创建一个EventState

    IEventState eventState = new StartedEvent();

  3. 对其调用StartedEvent.AddHost

    eventstate.AddHost(event,“ firstname”,“ lastname”);

  4. 检查event.HostSet ,检查它仅包含1个事件,并且此事件包含您提供的测试数据

我与你现有的测试看到的唯一问题是SetName_WhenCalled_ChangesTheEventNameCanSetName_WhenCalled_DelegatesToItsCurrentState没有Assert荷兰国际集团任何东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM