使用 WinForms MVP 模式的起订量 - 测试失败

发布于 2024-07-30 05:45:04 字数 2176 浏览 1 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2024-08-06 05:45:04

假设您的测试代码正在检查环境属性,您希望使用SetupGet而不是SetupSet(即告诉模拟在调用其环境属性时返回什么),

mockWindow.SetupGet(s => s.Environment).Returns(Environments.Test);

这是因为您没有在代码中设置该属性在做了。

或者,如果您想将环境属性视为标准属性,这就是您在编写“

mockWindow.Object.Environment = Environments.Test;

您可以使用”

mockWindow.SetupProperty(qf => qf.Environment);

之类的语句时所做的事情,我个人更喜欢第一种方法。

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)

mockWindow.SetupGet(s => s.Environment).Returns(Environments.Test);

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

mockWindow.Object.Environment = Environments.Test;

You can use

mockWindow.SetupProperty(qf => qf.Environment);

Personally I prefer the first approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文