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 に答える