0

我有以下使用 vanilla spec_helper 的示例代码。我正在尝试使用 sign_in_as,但收到一条错误消息,指出它不存在。我之前并没有真正看过这个配置语法,所以我不确定它是否被正确使用。任何帮助,将不胜感激?谢谢

在 helpers/sign_in_helpers.rb

module SignInHelpers
  def say_hello
    puts 'hello'
  end

  def sign_in
    sign_in_as 'person@example.com'
  end

  def sign_in_as email
    visit root_path
    fill_in 'Email address', with: email
    click_button 'Sign In'
  end
end     

RSpec.configure do |config|
  config.include SignInHelpers
end
4

2 回答 2

1

理论上,是的。您的调用看起来像是对 include 语法的有效使用

https://www.relishapp.com/rspec/rspec-core/docs/helper-methods/define-helper-methods-in-a-module

于 2013-04-06T04:09:59.667 回答
1

您的代码中有一些错误。

  1. 你应该在文件顶部有这个代码

    需要'spec_helper'

  2. 放置文件的地方是spec/support,不是spec/helpers

此外,对于以下代码,最好将它们全部保存在配置块内的 spec/spec_helper.rb 中,尽管您的方式也应该有效。

RSpec.configure do |config|
  config.include SignInHelpers
end

顺便说一句,最好将类型定义为您的助手仅用于功能,并且不适用于控制器测试。

config.include SignInHelpers type: :feature
于 2013-04-06T18:47:46.237 回答