Rspec/Capybara 测试中未定义模型方法,如果我 运行 自己在浏览器中测试则不会

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

这是我的 Rspec/Capybara 代码

visit new_user_registration_path
page.fill_in 'Email', with: 'manager@test.com'
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"

它returns这个错误:

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)>'

但是,如果我手动输入信息进行注册,则没有错误:

这是模型代码。

  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

有时迁移时测试数据库的架构不会更新。 运行 rake db:test:load 将从 db/schema.db 刷新测试数据库,通常可以解决问题。