1

所以我在rails中写了一个模块:

module SpecUtil

  def login
    visit tasks_path
    click_link "Sign in"
    current_path == log_in_path
    fill_in "email", :with => @user.email
    fill_in "password", :with => @user.password
    click_button "Save changes"
    current_path == tasks_path
    page.should have_content "You have logged in!"  
  end

  def create_comment
     visit tasks_path
     click_link @task.name
     current_path == task_path(@task)
     fill_in 'comment_comment', :with => 'I am a comment'
     click_button 'Create Comment'
  end

end

当我将 SpecUtil 包含到我的 rails 测试文件中时:

include SpecUtil

我得到错误

/home/adam/Documents/Aptana Studio 3 Workspace/StartPoint/spec/requests/categories_spec.rb:2:in `<top (required)>': uninitialized constant SpecUtil (NameError)

当我跑卫时会发生这种情况。文件 spec_util.rb 与测试存在于同一文件夹中......

为什么这会引起轰动?- 我在堆栈上读到,我包含模块的方式是正确的......

4

1 回答 1

1

通常像这样的实用程序模块被放入spec/support目录中。确保您spec/spec_helper.rb有加载支持文件的代码:

# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

并将您的模块移动到spec/support/spec_util.rb

于 2012-09-06T19:34:53.427 回答