【问题标题】:Rspec and Capybara uninitialized constantRspec 和 Capybara 未初始化常量
【发布时间】:2015-06-14 22:33:12
【问题描述】:

我在非 RoR 项目中使用 Rspec 3.2 和 Capybara 2.4。我正在尝试使用 Capybara gem 提供的功能模式进行测试。

$ cat .rspec                                                                                    
--color
--require spec_helper

$ cat spec/features/test_spec.rb                                                                
feature 'login' do
    username ="rspec#{Time.now.to_i}"
    valid_email = "#{username}@gmail.com"

    scenario 'with valid email' do
      sign_up_with  valid_email, 'pwd', 'pwd', username
      expect(page).to have_content('LOGOUT')
    end
  end

$ cat spec/support/session_helper.rb                                                            
module SessionHelper
  def sign_up_with(email, password, confirm_password, username)
      visit '/signup'
      fill_in 'email', with: email
      fill_in 'password', with: password
      fill_in 'passconf', with: confirm_password
      fill_in 'username', with: username
      click_button 'submit'
  end
end

$ cat spec/spec_helper.rb                                                                       
require 'capybara/rspec'
Capybara.default_driver = :selenium
RSpec.configure do |config|
  ...
  config.include SessionHelper, type: :feature
  ...
end

这是一个非 RoR 项目,当我运行测试时出现此错误:

$ rspec                                                                                         
spec/spec_helper.rb:26:in `block in <top (required)>': uninitialized constant SessionHelper (NameError)

在线文档中有很多示例,我按照示例构建了我的文件,但它不起作用。

【问题讨论】:

    标签: ruby testing rspec capybara bdd


    【解决方案1】:

    我在所有项目中的处理方式都是这样的:

    需要所有支持文件:

    spec_helper.rb

    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
    

    然后您的 spec/support/session_helper.rb 应该如下所示:

    module SessionHelper
      def sign_up_with(email, password, confirm_password, username)
          visit '/signup'
          fill_in 'email', with: email
          fill_in 'password', with: password
          fill_in 'passconf', with: confirm_password
          fill_in 'username', with: username
          click_button 'submit'
      end
    end
    
    RSpec.configure do |config|
      # Remove the equivalent line from spec_helper.rb
      config.include(SessionHelper)
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 2019-11-11
      • 1970-01-01
      相关资源
      最近更新 更多