2014-02-08 50 views
1

我最近嘗試安裝Cucumber來補充我的測試環境。我已經有了Guard和Spork和Rspec完美合作。我已經設置了一切,現在運行,包括黃瓜,但有兩件事情有點時髦。Rails:帶有Guard(+ spork和+ rspec)的黃瓜不能在更改時運行功能

第一期:當Guard運行Cucumber測試時,它在打印「禁用配置文件...」後暫停約10秒鐘。大約10秒後,它完全運行測試。 第二期:當我對我的功能(存儲在/features/users/sign_in.feature等子目錄中)或步驟定義進行更改時,guard不會運行測試。爲了讓我運行測試,我必須手動命中並且Guard繼續再次運行所有測試。顯然,這打破了守衛的目的。

下面是我的一些代碼:

保護端口輸出

[email protected]:~/rails_projects/katmeer4$ bundle exec guard 
16:51:57 - INFO - Guard is using Libnotify to send notifications. 
16:51:57 - INFO - Guard is using TerminalTitle to send notifications. 
16:51:57 - INFO - Guard::RSpec is running 
16:51:57 - INFO - Running all specs 
No DRb server is running. Running in local process instead ... 
.....[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message. 
........F.... 

Failures: 

    1) User abilities admin Index Page 
    Failure/Error: it {should have_selector('div', text: 'delete')} 
    Capybara::ExpectationNotMet: 
     expected to find css "div" with text "delete" but there were no matches 
    # ./spec/features/role_spec.rb:38:in `block (5 levels) in <top (required)>' 

Finished in 1.18 seconds 
18 examples, 1 failure 

Failed examples: 

rspec ./spec/features/role_spec.rb:38 # User abilities admin Index Page 


Randomized with seed 16984 

16:52:08 - INFO - Starting Spork for RSpec, Cucumber 
Using RSpec, Rails 
Using Cucumber, Rails 
Preloading Rails environment 
Preloading Rails environment 
Loading Spork.prefork block... 
Loading Spork.prefork block... 
Spork is ready and listening on 8990! 
Spork is ready and listening on 8989! 
16:52:18 - INFO - Spork server for RSpec, Cucumber successfully started 

16:52:18 - INFO - Running all features 
Disabling profiles... 
.. 
P- 

(::) pending steps (::) 

features/users/sign_in.feature:9:in `Then I see an invalid login message' 

1 scenario (1 pending) 
4 steps (1 skipped, 1 pending, 2 passed) 
0m0.827s 

重要Guardfile代碼

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(%r{features/support/}) { :cucumber } 
end 


guard 'cucumber' do 
    watch(%r{^features/.+\.feature$}) 
    watch(%r{^features/support/.+$})   { 'features' } 
    watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' } 
end 

功能/支持/ env.rb

require 'rubygems' 
require 'spork' 

Spork.prefork do 
end 

Spork.each_run do 
end 

require 'cucumber/rails' 

ActionController::Base.allow_rescue = false 
begin 
    DatabaseCleaner.strategy = :transaction 
rescue NameError 
    raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it." 
end 

Cucumber::Rails::Database.javascript_strategy = :truncation 

回答

0

您忘了將env.rb代碼拆分爲prefork和each_run塊。嘗試:

require 'rubygems' 
require 'spork' 

Spork.prefork do 
    require 'cucumber/rails' 
end 

Spork.each_run do 
    ActionController::Base.allow_rescue = false 

    begin 
    DatabaseCleaner.strategy = :transaction 
    rescue NameError 
    raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it." 
    end 

    Cucumber::Rails::Database.javascript_strategy = :truncation 
end 
+1

嗨丹,我做了上面的更改,但不幸的是,Guard仍然沒有接受我對文件所做的更改(關於黃瓜)。 – zenben1126