使用 WinForms MVP 模式的起订量 - 测试失败
我正在学习 TDD 和 MVP 模式。 我创建了一个简单的 WinForms 应用程序,它就像 TOAD SQL 工具的替代品。 我现在正尝试回去为我已经编写的代码编写测试(我知道这不是 TDD 的正确过程,但请耐心等待)。
在我的表单测试类中,我想测试具体的 Presenter
,但模拟 WinForm,因为演示者中有应该测试的真实逻辑。 但是,当我使用 Moq 模拟视图时,我没有看到预期的结果。 在下面的代码中,前 2 个测试通过,但第 3 个测试在第一个断言上失败。
当我将调试器附加到 NUnit 并运行时,调用 presenter.IsDangerousSQL
时,Environment
属性未设置为 Environments.Test
:
private Mock<IQueryForm> mockWindow;
private QueryFormPresenter presenter;
/// <summary>
/// Runs ONCE prior to any tests running
/// </summary>
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
//We're interested in testing the QueryFormPresenter class here, but we
//don't really care about the QueryForm window (view) since there is hardly any code in it.
//Therefore, we create a mock of the QueryForm view, and pass it to the QueryFormPresenter to use.
mockWindow = new Mock<IQueryForm>();
presenter = new QueryFormPresenter(mockWindow.Object);
}
[Test]
public void User_Can_Execute_Selects_From_Any_Environment()
{
Assert.AreEqual(false, presenter.IsDangerousSQL("select 1"));
}
[Test]
public void User_Cant_Execute_Schema_SQL_On_Any_Environment()
{
Assert.AreEqual(true, presenter.IsDangerousSQL("alter table"));
Assert.AreEqual(true, presenter.IsDangerousSQL("DrOp table"));
}
//Updates, Deletes and Selects are allowed in Test, but not in Production
[Test]
public void User_Can_Only_Execute_Updates_Or_Deletes_Against_Test()
{
//mockWindow.SetupSet(w => w.Environment = Environments.Test);
mockWindow.Object.Environment = Environments.Test;
Assert.AreEqual(false, presenter.IsDangerousSQL("update "));
Assert.AreEqual(false, presenter.IsDangerousSQL("delete "));
//mockWindow.SetupSet(w => w.Environment = Environments.Production);
//mockWindow.Object.Environment = Environments.Test;
Assert.AreEqual(true, presenter.IsDangerousSQL("update "));
Assert.AreEqual(true, presenter.IsDangerousSQL("delete "));
}
I非常感谢任何人都可以提供的见解! 另外,IsDangerousSQL 方法实际上应该在 Model 类中吗,因为它代表业务逻辑而不是直接对用户操作做出反应?
谢谢!!
安迪
I'm learning TDD and the MVP pattern. I've created a simple WinForms app that's like a replacement for the TOAD SQL tool. I'm trying to go back and write tests now for the code I've already written (which I know isn't the correct process for TDD, but please bear with me).
In my test class for the form, I want to test the concrete Presenter
, but mock out the WinForm as the presenter has real logic in it that should be tested. However, when I mock the view using Moq, I'm not seeing expected results. In the code below, the first 2 tests PASS, but the 3rd FAILS on the first Assert.
When I attach the debugger to NUnit and run, the Environment
property isn't set to Environments.Test
when presenter.IsDangerousSQL
is called:
private Mock<IQueryForm> mockWindow;
private QueryFormPresenter presenter;
/// <summary>
/// Runs ONCE prior to any tests running
/// </summary>
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
//We're interested in testing the QueryFormPresenter class here, but we
//don't really care about the QueryForm window (view) since there is hardly any code in it.
//Therefore, we create a mock of the QueryForm view, and pass it to the QueryFormPresenter to use.
mockWindow = new Mock<IQueryForm>();
presenter = new QueryFormPresenter(mockWindow.Object);
}
[Test]
public void User_Can_Execute_Selects_From_Any_Environment()
{
Assert.AreEqual(false, presenter.IsDangerousSQL("select 1"));
}
[Test]
public void User_Cant_Execute_Schema_SQL_On_Any_Environment()
{
Assert.AreEqual(true, presenter.IsDangerousSQL("alter table"));
Assert.AreEqual(true, presenter.IsDangerousSQL("DrOp table"));
}
//Updates, Deletes and Selects are allowed in Test, but not in Production
[Test]
public void User_Can_Only_Execute_Updates_Or_Deletes_Against_Test()
{
//mockWindow.SetupSet(w => w.Environment = Environments.Test);
mockWindow.Object.Environment = Environments.Test;
Assert.AreEqual(false, presenter.IsDangerousSQL("update "));
Assert.AreEqual(false, presenter.IsDangerousSQL("delete "));
//mockWindow.SetupSet(w => w.Environment = Environments.Production);
//mockWindow.Object.Environment = Environments.Test;
Assert.AreEqual(true, presenter.IsDangerousSQL("update "));
Assert.AreEqual(true, presenter.IsDangerousSQL("delete "));
}
I greatly appreciate any insights anyone can offer! And also, should the IsDangerousSQL
method actually be in a Model class since it represents business logic rather than directly reacting to a user action?
Thanks!!
Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的测试代码正在检查环境属性,您希望使用SetupGet而不是SetupSet(即告诉模拟在调用其环境属性时返回什么),
这是因为您没有在代码中设置该属性在做了。
或者,如果您想将环境属性视为标准属性,这就是您在编写“
您可以使用”
之类的语句时所做的事情,我个人更喜欢第一种方法。
Assuming your code under test is checking the Environment property you'd want to use SetupGet instead of SetupSet (i.e. tell the mock what to return when its environment property is called)
This because you're not setting the property in the code you're getting it.
Alternatively if you'd like to treat the Environment property as a standard property, which is what you're doing when you write statements like
You can use
Personally I prefer the first approach.