所以我在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与测试文件存在于同一文件夹中。
为什么会出现这种情况? - 我在堆栈上读到,我包含模块的方式是正确的......
答案 0 :(得分: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