[英]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
:
建立Event
事件event = new Event();
創建一個EventState
IEventState eventState = new StartedEvent();
對其調用StartedEvent.AddHost
:
eventstate.AddHost(event,“ firstname”,“ lastname”);
event.HostSet
,檢查它僅包含1個事件,並且此事件包含您提供的測試數據 我與你現有的測試看到的唯一問題是SetName_WhenCalled_ChangesTheEventName
和CanSetName_WhenCalled_DelegatesToItsCurrentState
沒有Assert
荷蘭國際集團任何東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.