Capybara 2.0和rspec-rails -- helpers在spec/features中无法使用

5
我正在尝试使用来自helper模块的方法,但是rspec似乎不认识在spec/features下的测试所需的helper。请注意,spec_helper.rb唯一的更改是添加了require 'capybara/rspec'。
我尝试将helper.rb移动到spec/support、spec/helpers和spec/features(包含我的测试的目录),但都没有成功。测试表明helper方法未定义。
唯一能够“工作”的方法是将我的测试移动到其他目录,例如spec/integration。但现在capybara无法工作(visit undefined),因为它不在spec/features中。
这是我的helper模块(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

1
你试过了吗:c.include AuthenticationHelper, type: :feature - apneadiving
我刚试了一下,但是我仍然遇到同样的问题。 - user1164937
我找到了一个解决方案:我将*_spec.rb文件移动到spec/integration目录,并在spec_helper中添加了config.include Capybara::DSL。似乎我甚至可以将*_spec.rb文件移动到spec/requests目录。但是我仍然有点担心这不是标准的做法,因为capybara 2建议将spec文件放入spec/features目录。但与此同时,我对软件开发世界还很陌生,所以我可能在担心无谓的事情。我不知道,你们觉得呢?我应该寻找更好的方法还是保持现状? - user1164937
哇哦。我非常确定我尝试了你建议的方法——我甚至记得它是:feature而不是:features,因为我进行了双重检查。这可能与rspec-rails 2.13.1有关,因为在将其更新为2.13.2之后,它可以正常工作!谢谢!不过,我不知道如何将评论视为答案。 - user1164937
1个回答

0
将这个辅助文件放在spec/support/目录下
并添加以下代码到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 }

spec_helper.rb 已经默认包含了那个,不幸的是。我对 spec_helper.rb 文件唯一的修改就是添加了 require 'capybara/rspec'。 - user1164937
在完成上述步骤后,将“include AuthenticationHelper”添加到require语句中。 - Sachin Singh
似乎它不能工作,但我理解你的意思。不幸的是,这并不是我所寻找的解决方案,因为可能还需要使用该模块的其他测试。尽管可行,但我更愿意使用RSpec提供的功能(目前似乎无法直接使用),以便让这个过程对我和其他人更加简单。 - user1164937

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接