Peter R
Peter R

Reputation: 3506

Rspec/Capybara undefined model method in test, not if I run the test myself in the browser

Here's my Rspec/Capybara code

visit new_user_registration_path
page.fill_in 'Email', with: '[email protected]'
page.fill_in 'Password (8 characters minimum)', with: 'Password'
page.fill_in 'Password confirmation', with: 'Password'
page.fill_in 'Promo code', with: "test100"
page.click_button "Sign up"

It returns this error:

1) Sign up Sign in with promo code when new user signs up with 100% promo
     Failure/Error: page.click_button "Sign up"
     NoMethodError:
       undefined method `active=' for #<Subscription:0x007f9b5f868c68>
     # ./app/models/subscription.rb:24:in `activate'
     # ./app/models/subscription.rb:17:in `setup'
     # ./app/controllers/users/registrations_controller.rb:37:in `create'
     # ./spec/integration/signup_spec.rb:18:in `block (3 levels) in <top (required)>'

However if I manually enter the info to sign up there's no error:

Here is the model code.

  def setup
    if has_full_discount?
      activate
    elsif create_stripe_customer and charge_stripe_customer
      activate
    end
  end

  def activate
    self.update_attribute(:active, true)
  end

Upvotes: 0

Views: 58

Answers (1)

max
max

Reputation: 101811

Sometimes the schema of your test database is not updated when migrating. Running rake db:test:load will refresh the test database from db/schema.db and usually resolves the issue.

Upvotes: 1

Related Questions