Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this spec failing, when the conditions it's testing pass in development?
    primarykey
    data
    text
    <p>I have a <code>User</code> model and <code>Authentications</code> model, which is a basic omniauth setup. Essentially, users can sign up through oauth without setting a password.</p> <p>I have a <code>Authentication.is_destroyable?</code> method that returns true if the user has a password or has more than one authentication. Essentially, this prevents users deleting their one and only way of authentication.</p> <pre><code>def is_destroyable? if user.encrypted_password.present? || user.authentications.count &gt; 1 true else errors.add :base, 'not allowed' false end end </code></pre> <p>When testing this in development it works as expected under all conditions. However, my unit tests are failing:</p> <pre><code>describe "Authentication#is_destroyable?" do before(:each) do # This creates a user with no password and a single authentication @user = FactoryGirl.create(:user_with_oauth) @auth = @user.authentications.first end # This spec passes :) it "should return false when is users only authentication method" do @auth.is_destroyable?.should be_false end # This FAILS - I have no idea why :( it "should return true when user has multiple authentications" do @user.authentications.create FactoryGirl.attributes_for(:authentication, :provider =&gt; 'twitter') @auth.is_destroyable?.should be_true end # This FAILS - I have no idea why :( it "should return true when user has a password" do @user.update_attributes :password =&gt; 'password' @auth.is_destroyable?.should be_true end end </code></pre> <p>I've spent the best part of 3 hours banging my head against the wall. I can't for the life of me understand why this works when I manually test the functionality (and Cucumber stories pass also testing the functionality), but in rspec the unit tests are failing. Is there something obvious that I'm missing?</p> <h3>Edit</h3> <p>As requested, here's some further detail.</p> <p>Both failing specs fail with: </p> <pre><code>Failure/Error: @auth.is_destroyable?.should be_true expected false to be true </code></pre> <p>The Factories:</p> <pre><code>FactoryGirl.define do factory :user do username { FactoryGirl.generate(:username) } name 'Test User' email { FactoryGirl.generate(:email) } password 'password' end factory :user_with_oauth, :parent =&gt; :user do password nil authentications [ FactoryGirl.build(:authentication) ] end factory :authentication do provider 'facebook' uid SecureRandom.hex(16) end end </code></pre> <p>Also, maybe relevant, am using DatabaseCleaner with the <em>truncation</em> strategy.</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. 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