Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The context looks good. I like the way you solved the conflicting <code>It</code> with aliases. I would argue that the Moq alias can be improved. Consider something sentence-like. For example, <code>Param.Is&lt;T&gt;</code> or <code>Value.Is&lt;T&gt;</code>.</p> <p>Some notes, with code snippets, then the whole spec rewritten at the bottom.</p> <h3>The Scenario is your <code>Subject</code></h3> <p>The Subject can be the Scenario from the story. Plus, it gets rendered with your test run report (especially nice in the HTML report).</p> <pre><code>[Subject("Login Page")] </code></pre> <h3>Don't waste time on "With" named base classes</h3> <p>MSpec's creator, Aaron Jensen, <a href="http://codebetter.com/blogs/aaron.jensen/archive/2009/10/05/a-recent-conversation-about-mspec-practices.aspx" rel="nofollow noreferrer">has reverted</a> from using the "With" syntax altogether. Context class names do not show up for any reports, so avoid spending time inventing a meaningful name.</p> <pre><code>public abstract class MembershipContext </code></pre> <h3>The Given is your spec class name</h3> <p>Name the concrete spec class after the Given in your story. Especially since the base class name isn't reported anywhere, you could be losing half your context in the report! You should also avoid putting the name of the system under test in context class names. This makes your contexts friendlier to refactoring the system under test.</p> <pre><code>public class When_an_existing_user_enters_valid_credentials </code></pre> <h3>Base spec classes should contain only general initialization</h3> <p>And are often unnecessary. They lead to separation of the Arrange and Act phases. Use a base class for common field initialization, like setting up mocked dependencies. But, you should not mock behavior in a base class. And you should not put context-specific information in the base class. In your example, the username/password. This way, you can create a second context with invalid credentials.</p> <pre><code>Establish context = () =&gt; { membership = new Mock&lt;ISiteMembership&gt;(); loginController = new LoginController(membership.Object); }; </code></pre> <h3>Fields in the concrete spec class should be private</h3> <p>It reduces the "ceremony" of the language in your test. You should place them below all of the MSpec specific delegates, as those parts of the spec tell most of the story.</p> <pre><code>static ActionResult result; </code></pre> <h1>The Spec Overhaul</h1> <p>The spec here is an excellent example of establishing a global context <code>MembershipContext</code> and inheriting it in a context specific to the spec (thus, the additional <code>Establish</code>).</p> <pre><code>[Subject("Login Page")] public class When_an_existing_user_enters_valid_credentials : MembershipContext { Establish context = () =&gt; { membership .Setup&lt;bool&gt;(m =&gt; m.Validate( Param.Is&lt;string&gt;(s =&gt; s == username), Param.Is&lt;string&gt;(s =&gt; s == password))) .Returns(true); }; Because of = () =&gt; result = loginController.Login(username, password); It should_log_the_user_in; It should_redirect_the_user_to_the_admin_panel; It should_show_message_confirming_successful_login; static ActionResult result; const string username = "username"; const string password = "password"; } public abstract class MembershipContext { Establish context = () =&gt; { membership = new Mock&lt;ISiteMembership&gt;(); loginController = new LoginController(membership.Object); }; protected static Mock&lt;ISiteMembership&gt; membership; protected static LoginController loginController; } </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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