新手问题 rspec 自定义方法的问题

williamherry · 2012年11月10日 · 最后由 Shallow__pace 回复于 2013年06月28日 · 2684 次阅读

看 Ruby on Rails Tutorial 遇到的问题,它说可以在spec/support/utilities.rb中定义方法,然后在spec/requests/user_pages_spec.rb文件中可以调用

utilities.rb 文件

include ApplicationHelper                                                                                 

def valid_signin(user)
  fill_in "Email", with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"
end

Rspec::Matchers.define :have_error_message do |message|
  match do |page|
    page.should have_selector('div.alert.alert-error', text: message)
  end
end

def sign_in(user)
  fill_in "Email", with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"
  # Sign in when not using Capybara as well.
  cookies[:remember_token] = user.remember_token
end

但调用的时候出现

undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1::Nested_2:0x007f91a4710d80>

有人能帮我讲讲吗?

一般要自己 require 吧

Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}

如果要加方法最好是用 module,然后在 spec_helper 里面 include,详细看 rspec 的文档 https://www.relishapp.com/rspec/rspec-core/docs/helper-methods/define-helper-methods-in-a-module

同这个问题。。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号