Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The immediate problem you're having is how you're setting up your tests. The subject is being set as the unsaved instantiation of the object. If you were to do a <code>before { subject.reload }</code> after your <code>@user.save</code> then it would fix this. That's really just a bandaid.</p> <p>The real problem is you're <strong>over complicating</strong> your spec. Your setting the same user in three different places in three different ways in three different states. This should never happen.</p> <p>Here's how I'd set this up:</p> <pre><code>require 'spec_helper' describe User do describe "#authenticate" do let(:user) { User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar") } it "has a valid password" do user.authenticate("foobar").should be_true end it "has invalid password" do user.authenticate("invalid").should be_false end end end </code></pre> <p>Just a couple of notes to keep in mind.</p> <p><strong>Create as few objects as possible:</strong> The more you create the more likely something isn't needed or is in a bad state and your tests fail or even worse your tests are fragile and fail unexpectedly.</p> <p><strong>Single line <code>it</code> is fine, but limiting:</strong> Single line <code>it</code> blocks are great when you need to do some quick validation, but you should use them sparingly. If you find that you're wrapping an <code>it</code> block in a <code>describe</code> then get rid of that <code>describe</code></p> <p><strong>Keep it simple:</strong> Try not to front load your tests. When you front load more than likely you're writing tests that conform to the data.</p> <p>There a bunch of other great tips over at <a href="http://betterspecs.org/" rel="nofollow">http://betterspecs.org/</a>. By no means take this as law, but it does have some good information on setting a really good foundation for writing specs.</p> <p>Lastly, good luck and enjoy writing specs!</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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