Note that there are some explanatory texts on larger screens.

plurals
  1. POMoq with WinForms MVP Pattern - Failing Test
    primarykey
    data
    text
    <p>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).</p> <p>In my test class for the form, I want to test the concrete <code>Presenter</code>, 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.</p> <p>When I attach the debugger to NUnit and run, the <code>Environment</code> property isn't set to <code>Environments.Test</code> when <code>presenter.IsDangerousSQL</code> is called:</p> <pre><code>private Mock&lt;IQueryForm&gt; mockWindow; private QueryFormPresenter presenter; /// &lt;summary&gt; /// Runs ONCE prior to any tests running /// &lt;/summary&gt; [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&lt;IQueryForm&gt;(); 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 =&gt; w.Environment = Environments.Test); mockWindow.Object.Environment = Environments.Test; Assert.AreEqual(false, presenter.IsDangerousSQL("update ")); Assert.AreEqual(false, presenter.IsDangerousSQL("delete ")); //mockWindow.SetupSet(w =&gt; w.Environment = Environments.Production); //mockWindow.Object.Environment = Environments.Test; Assert.AreEqual(true, presenter.IsDangerousSQL("update ")); Assert.AreEqual(true, presenter.IsDangerousSQL("delete ")); } </code></pre> <p>I greatly appreciate any insights anyone can offer! And also, should the <code>IsDangerousSQL</code> method actually be in a Model class since it represents business logic rather than directly reacting to a user action?</p> <p>Thanks!!</p> <p>Andy</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload