Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this RSpec test failing?
    text
    copied!<p>I'm in the process of learning Ruby on Rails, so treat me like a total neophyte, because I am.</p> <p>I've got a User model with some associated RSpec tests, and the following test fails:</p> <pre><code>require 'spec_helper' describe User do it 'should require a password' do User.new({:email =&gt; 'valid_email@example.com', :password =&gt; '', :password_confirmation =&gt; ''}).should_not be_valid end end </code></pre> <p>The relevant part of the <code>User</code> model looks like this:</p> <pre><code>class User &lt; ActiveRecord::Base ... validates :password, :presence =&gt; true, :confirmation =&gt; true, :length =&gt; { :minimum =&gt; 6 } ... end </code></pre> <p>Here's the catch: if I run <code>User.new(...).valid?</code> from a Rails console using the arguments above, it returns false as expected and shows the correct errors (password is blank).</p> <p>I was using spork/autotest and I restarted both to no avail, but this test also fails even running it directly with <code>rspec</code>. What am I doing wrong here?</p> <p><strong>EDIT</strong></p> <p>I tried a few more things with the test. This fails:</p> <pre><code> u = User.new({:email =&gt; 'valid_email@example.com', :password =&gt; '', :password_confirmation =&gt; ''}) u.should_not be_valid </code></pre> <p>So does this:</p> <pre><code> u = User.new({:email =&gt; 'valid_email@example.com', :password =&gt; '', :password_confirmation =&gt; ''}) u.valid? u.errors.should_not be_empty </code></pre> <p>This passes, confirming that <code>:password</code> is indeed blank:</p> <pre><code> u = User.new({:email =&gt; 'valid_email@example.com', :password =&gt; '', :password_confirmation =&gt; ''}) u.password.should == '' </code></pre>
 

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