问题描述
我有以下课程
public delegate void OnInvalidEntryMethod(ITnEntry条目,字符串消息);
public class EntryValidator
{
public event OnInvalidEntryMethod OnInvalidEntry;
public bool IsValidEntry(ITnEntry条目,字符串代码)
{
if(!IsFieldValid(entry,ticker.Trim()。Length.ToString(),0) )
return false;
返回true;
}
私人bool IsFieldValid(ITnEntry条目,字符串实际,字符串无效值)
{
if(actual == invalidValue)
{
RaiseInvalidEntryEvent(entry);
返回false;
}
返回true;
private void RaiseInvalidEntryEvent(ITnEntry条目)
{
if(OnInvalidEntry!= null)
OnInvalidEntry(条目列表中的无效条目: + entry.List.Name +。);
}
}
我已经写了测试案例,但是正在努力事件和委托如下所示
[TestFixture]
public class EntryValidatorTests
{
private EntryValidator _entryValidator;
private FakeTnEntry _selectedEntry;
私人字符串_ticker;
[SetUp]
public void Setup()
{
_entryValidator = new EntryValidator();
_ticker =BOL;
}
private FakeTnEntry MakeEntry(string ticker)
{
返回新的FakeTnEntry {Ticker = ticker};
}
[测试]
public void IsValidEntry_WithValidValues()
{
_selectedEntry = MakeEntry(_ticker);
Assert.IsTrue(_entryValidator.IsValidEntry(_selectedEntry,_selectedEntry.Ticker));
}
[测试]
public void IsValidEntry_WithInValidTicker()
{
_selectedEntry = MakeEntry();
Assert.IsFalse(_entryValidator.IsValidEntry(_selectedEntry,_selectedEntry.Ticker));
}
}}
有人可以帮忙吗?谢谢..
使用匿名方法订阅该事件可能最简单:
[Test]
public void IsValidEntry_WithValidValues()
{
_selectedEntry = MakeEntry(_ticker);
_entryValidator.OnInvalidEntry + = delegate {
Assert.Fail(不应该被调用);
};
Assert.IsTrue(_entryValidator.IsValidEntry(_selectedEntry,_selectedEntry.Ticker));
}
[测试]
public void IsValidEntry_WithInValidTicker()
{
bool eventRaised = false;
_selectedEntry = MakeEntry();
_entryValidator.OnInvalidEntry + = delegate {eventRaised = true; };
Assert.IsFalse(_entryValidator.IsValidEntry(_selectedEntry,_selectedEntry.Ticker));
Assert.IsTrue(eventRaised);
}
在第二个测试中,您可能需要验证事件参数是否符合预期
另请注意,无效是一个字,因此您的测试应为 IsValidEntry_WithInvalidTicker
。我也没有打扰设置 - 我只是在每个测试中声明新的局部变量。
I am new to testing please help.
I have the following class
public delegate void OnInvalidEntryMethod(ITnEntry entry, string message);
public class EntryValidator
{
public event OnInvalidEntryMethod OnInvalidEntry;
public bool IsValidEntry(ITnEntry entry, string ticker)
{
if (!IsFieldValid(entry, ticker.Trim().Length.ToString(), "0"))
return false;
return true;
}
private bool IsFieldValid(ITnEntry entry, string actual, string invalidValue)
{
if (actual == invalidValue)
{
RaiseInvalidEntryEvent(entry);
return false;
}
return true;
}
private void RaiseInvalidEntryEvent(ITnEntry entry)
{
if (OnInvalidEntry != null)
OnInvalidEntry(entry, "Invalid entry in list: " + entry.List.Name + ".");
}
}
I have written the test case so far but am struggling with the event and delegate as shown below
[TestFixture]
public class EntryValidatorTests
{
private EntryValidator _entryValidator;
private FakeTnEntry _selectedEntry;
private string _ticker;
[SetUp]
public void Setup()
{
_entryValidator = new EntryValidator();
_ticker = "BOL";
}
private FakeTnEntry MakeEntry(string ticker)
{
return new FakeTnEntry { Ticker = ticker};
}
[Test]
public void IsValidEntry_WithValidValues()
{
_selectedEntry = MakeEntry(_ticker);
Assert.IsTrue(_entryValidator.IsValidEntry(_selectedEntry, _selectedEntry.Ticker));
}
[Test]
public void IsValidEntry_WithInValidTicker()
{
_selectedEntry = MakeEntry("");
Assert.IsFalse(_entryValidator.IsValidEntry(_selectedEntry, _selectedEntry.Ticker));
}
}}
Please can someone help? Thanks..
It's probably simplest just to subscribe to the event using an anonymous method:
[Test]
public void IsValidEntry_WithValidValues()
{
_selectedEntry = MakeEntry(_ticker);
_entryValidator.OnInvalidEntry += delegate {
Assert.Fail("Shouldn't be called");
};
Assert.IsTrue(_entryValidator.IsValidEntry(_selectedEntry, _selectedEntry.Ticker));
}
[Test]
public void IsValidEntry_WithInValidTicker()
{
bool eventRaised = false;
_selectedEntry = MakeEntry("");
_entryValidator.OnInvalidEntry += delegate { eventRaised = true; };
Assert.IsFalse(_entryValidator.IsValidEntry(_selectedEntry, _selectedEntry.Ticker));
Assert.IsTrue(eventRaised);
}
In the second test you might want to validate that the event arguments were as expected too.
Also note that "invalid" is one word - so your test should be IsValidEntry_WithInvalidTicker
. I'd also not bother with the setup - I'd just declare new local variables in each test.
这篇关于单元测试一个类与事件和委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!