Capybara 2.0和rspec-rails-帮助程序在spec / features中不起作用 [英] Capybara 2.0 and rspec-rails -- helpers don't work in spec/features

查看:83
本文介绍了Capybara 2.0和rspec-rails-帮助程序在spec / features中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用帮助程序模块中的方法,但是rspec似乎无法识别 spec / features 下的测试的帮助程序。请注意,对 spec_helper.rb 的唯一更改是添加了 require'capybara / rspec'

I'm trying to use a method from a helper module, but rspec doesn't seem to recognize helpers for tests under spec/features. Note that the only change to spec_helper.rb was adding require 'capybara/rspec'.

我尝试将 helper.rb 移至 spec / support spec / helpers spec / features (包含我的测试的目录),但没有运气。测试不断表明该辅助方法未定义。

I tried moving helper.rb to spec/support, spec/helpers, and spec/features (the directory that contains my tests), but no luck. The test keeps indicating that the helper method is undefined.

使它工作的唯一方法是将测试移至另一个目录,例如 spec / integration 。但是现在水豚无法工作(访问未定义),因为它不在规范/功能中。

The only way to get it to "work" was by moving my test to a different directory, such as spec/integration. But now capybara won't work (visit undefined) because it's not in spec/features.

这是我的帮助程序模块( authentication_helper.rb ):

Here's my helper module (authentication_helper.rb):

module AuthenticationHelper
    def sign_in_as!(user)
        visit '/users/sign_in'
        fill_in "Email", with: user.email
        fill_in "Password", with: "password"
        click_button "Sign in"
        page.should have_content("Signed in successfully.")
    end
end

RSpec.configure do |c|
    c.include AuthenticationHelper, type: :request
end


推荐答案

将此帮助文件放入spec / support /

put this helper file in spec/support/

和这一行spec_helper.rb

and this line spec_helper.rb

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

这篇关于Capybara 2.0和rspec-rails-帮助程序在spec / features中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆