[英]Unit Testing using Moq fails
I am new to the unit testing world so please spare me.我是单元测试领域的新手,所以请原谅我。 The goal of the question is to test a class as mentioned below:
问题的目标是测试如下所述的类:
public class Engine
{
#region Members
private EnumDefinition.EngineRunningState runningState;
private ComponentController componentController;
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
#endregion
#region Constructor
public Engine(ComponentController componentController)
{
this.componentController = componentController;
}
#endregion
#region Properties
public EnumDefinition.EngineRunningState RunningState
{
get
{
return runningState;
}
private set
{
if (value != runningState)
{
componentController.EngineRunningStateChangedEvent += OnEngineRunningStateChange;
}
}
}
#endregion
/// <summary>
/// Start the Engine Request
/// </summary>
public void StartEngineRequest()
{
// Check Engine Status
if (runningState == EnumDefinition.EngineRunningState.Off)
{
componentController.SetEngineRequestToActive();
}
else if (runningState == EnumDefinition.EngineRunningState.Error)
{
logger.Trace("Engine Start Request Sent on a ENGINE ERROR!!!!!!");
}
else
{
logger.Trace("Engine Start Request, Engine is still ON");
}
}
private void OnEngineRunningStateChange(object sender, EngineRunningStateArgs e)
{
runningState = e.runningStateofEngine;
}
}
I want to test the public functions in Engine.我想测试引擎中的公共功能。 Now I present my Test class:
现在我介绍我的测试类:
[TestFixture]
public class EngineTest
{
[Test]
public void StartEngineRequestTest()
{
Mock<ComponentController> mockComponentController = new Mock<ComponentController>();
Mock<EngineRunningStateArgs> mockEngineRunningStateArgs =
new Mock<EngineRunningStateArgs>(EnumDefinition.EngineRunningState.On);
var engineTest = new Engine(mockComponentController.Object);
mockComponentController.Raise(mock => mock.EngineRunningStateChangedEvent += null, mockEngineRunningStateArgs.Object);
engineTest.StartEngineRequest();
}
}
mt test fails giving an error as mentioned below: Result StackTrace: at Moq.Extensions.GetEvent[TMock](Action 1 eventExpression, TMock mock) at Moq.Mock 1.Raise(Action 1 eventExpression, EventArgs args) at AutoTugTest.EngineTest.StartEngineRequestTest() in C:\\GatewayController\\01 Working Copy\\GatewayComputer\\AutoTugTest\\EngineTest.cs:line 23 Result Message: System.ArgumentException : Expression is not an event attach or detach, or the event is declared in a class but not marked virtual.
mt 测试失败,给出如下错误:
Result StackTrace: at Moq.Extensions.GetEvent[TMock](Action 1 eventExpression, TMock mock) at Moq.Mock 1.Raise(Action 1 eventExpression, EventArgs args) at AutoTugTest.EngineTest.StartEngineRequestTest() in C:\\GatewayController\\01 Working Copy\\GatewayComputer\\AutoTugTest\\EngineTest.cs:line 23 Result Message: System.ArgumentException : Expression is not an event attach or detach, or the event is declared in a class but not marked virtual.
I have no idea what is happening.我不知道发生了什么。 I just want to throw an event from
ComponentController
with event name as EngineRunningStateChangedEvent
with an event argument.我只想从
ComponentController
抛出一个事件,事件名称为EngineRunningStateChangedEvent
并带有事件参数。 Please suggest.请建议。
Moq can only mock virtual members. Moq 只能模拟虚拟成员。 It's good at mocking interfaces because there's no implementations, but if you want to mock a concrete class the members that you mock will need to be virtual.
它擅长模拟接口,因为没有实现,但是如果你想模拟一个具体的类,你模拟的成员需要是虚拟的。 That lets Moq wrap the 'real' implementation with it's own.
这让 Moq 用它自己的方式包装“真正的”实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.