2012-11-21 57 views
4

我有一個奇怪的錯誤,我的spec/support文件夾中有一個utilities.rb文件來存放一些幫助函數,例如登錄方法。爲什麼我的規格/支持文件沒有被rspec拾取?

規格/支持/ utilities.rb

include ApplicationHelper 


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

我guardfile:

require 'active_support/core_ext' 


guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do 
    watch('config/application.rb') 
    watch('config/environment.rb') 
    watch('config/environments/test.rb') 
    watch(%r{^config/initializers/.+\.rb$}) 
    watch('Gemfile') 
    watch('Gemfile.lock') 
    watch('spec/spec_helper.rb') { :rspec } 
    watch('test/test_helper.rb') { :test_unit } 
    watch('spec/support/') 
    watch(%r{^spec/support/.+\.rb$}) 
    watch(%r{features/support/}) { :cucumber } 
end 

guard 'rspec', :version => 2, :all_after_pass => false, :cli => '--drb' do 
    watch(%r{^spec/.+_spec\.rb$}) 
    watch(%r{^lib/(.+)\.rb$})  { |m| "spec/lib/#{m[1]}_spec.rb" } 
    watch('spec/spec_helper.rb') { "spec" } 

    # Rails example 
    watch(%r{^app/(.+)\.rb$})       { |m| "spec/#{m[1]}_spec.rb" } 
    watch(%r{^app/(.*)(\.erb|\.haml)$})     { |m| "spec/#{m[1]}#{m[2]}_spec.rb" } 
    watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] } 
    watch(%r{^spec/support/(.+)\.rb$})     { "spec" } 
    watch('config/routes.rb')       { "spec/routing" } 
    watch('app/controllers/application_controller.rb') { "spec/controllers" } 

    # Capybara request specs 
    watch(%r{^app/views/(.+)/.*\.(erb|haml)$})   { |m| "spec/requests/#{m[1]}_spec.rb" } 

    # Turnip features and steps 
    watch(%r{^spec/acceptance/(.+)\.feature$}) 
    watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' } 

    watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m| 
    ["spec/routing/#{m[1]}_routing_spec.rb", 
    "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", 
    "spec/acceptance/#{m[1]}_spec.rb", 
    (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" : 
         "spec/requests/#{m[1].singularize}_pages_spec.rb")] 
    end 
    watch(%r{^app/views/(.+)/}) do |m| 
    (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" : 
         "spec/requests/#{m[1].singularize}_pages_spec.rb") 
    end 

end 

在我的集成測試的要求/ link_pages_spec.rb我嘗試做這樣的事情,但它給了我一個錯誤

require 'spec_helper' 

describe "Link pages" do 

    subject { page } 

    describe 'creating a link' do 

    before do 
     sign_in FactoryGirl.create(:user) 
     visit new_link_path 
    end 

    describe "with invalid information" do 
     it "should not create a link" do 
     expect { click_button 'Create Link' }.not_to change(Link, :count) 
     end 
    end 

    end 

end 

錯誤:

Failure/Error: sign_in FactoryGirl.create(:user) 
    NoMethodError: 
     undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007f8ebb563e10> 
    # ./spec/support/utilities.rb:5:in `sign_in' 
    # ./spec/requests/link_pages_spec.rb:10:in `block (3 levels) in <top (required)>' 

奇怪的是,如果我刪除的描述塊在我的鏈接頁面SPEC,沒有錯誤..

回答

1

我有同樣的問題。但是,嘗試了一個稍微不同的修復:我按照您所包含的鏈接推薦將'spec/requests'文件夾重命名爲'spec/features'。看到這裏以及:https://github.com/rspec/rspec-rails/blob/master/Capybara.md

之後,我所有的測試在Guard/Spork以及Sublime Text測試插件中運行綠色。

4

此外,如果您正在運行Spork,則通常需要重新啓動才能重新加載支持目錄中文件的更改。

+0

有類似的問題,只需重新啓動spork(通過重新啓動警衛)修復問題。 – RedBassett

相關問題