2013-06-04 35 views
7

我已經安裝了rspec-rails gem。當我運行rails g model movie showtime_date:date showtime_time:time我想,我應該得到Rspec在Rails 4 beta中沒有生成* _spec.rb文件

invoke active_record 
     create db/migrate/20130604010556_create_movies.rb 
     create app/models/movie.rb 
     invoke rspec 
     create spec/models/movie_spec.rb 

但是當我運行rails g model movie showtime_date:date showtime_time:time 我得到

invoke active_record 
     create db/migrate/xxxxxxxxxxx_create_movies.rb 
     create app/models/movie.rb 
     invoke test_unit 
     create  test/models/movie_test.rb 
     create  test/fixtures/movies.yml 

是否與我的gem包有問題?我正在使用Rails4測試版。這個版本是否有錯誤或其他?

的BDD我的應用程序是這樣的app/features/show_descripitons.feature

Feature: Showtime Descriptions 
    As a movie goer 
    I want to see accurate and concise showtimes 
    So that I can find movies that fit my schedule 

    @wip 
    Scenario: Show minutes for times ending with 00 
     Given a movie 
     When I set the showtime to "2013-05-04" at "2:15pm" 
     Then the showtime description should be "June 04, 2013(2pm)" 

    Scenario: Hide minutes for times ending with 00 
     Given a movie 
     When I set the showtime to "2013-05-04" at "2:00pm" 
     Then the showtime description should be "June 04, 2013(2pm)" 

和我app/features/step_definition/showtime_descriptions.rb是這個

Given(/^a movie$/) do 
    @movie = Movie.create! 
end 

When(/^I set the showtime to "(.*?)" at "(.*?)"$/) do |date, time| 
    @movie.update_attribute(:showtime_date, Date.parse(date)) 
    @movie.update_attribute(:showtime_time, time) 
end 

Then(/^the showtime description should be "(.*?)"$/) do |showtime| 
    @movie.showtime.showtime eq(showtime) 
end 
+0

你在開始時運行'rails generate rspec:install'嗎? –

回答

10

我應該把我這個代碼的config/application.rb中

config.generators do |g| 
     g.template_engine :erb 
     g.test_framework :rspec, :fixture => true, :views => false 
     g.integration_tool :rspec, :fixture => true, :views => true 
     g.fixture_replacement :factory_girl, :dir => "spec/support/factories" 
    end 
+0

我第一次嘗試使用omit測試單元選項(-T,[--skip-test-unit])[Aaron K ]在創建我的新導軌項目時建議。直到我將上面的代碼添加到我的application.rb中,這本身並不起作用。 – Jeremiah

+0

這工作很好,除了g.fixture_replacement導致消息「factory_girl [未找到]」我使用factory_girl目前的規格,所以寶石已安裝,它的工作;不知道爲什麼這會導致錯誤消息。 – Greg

+0

評論了這個https:// github。com/thoughtbot/factory_girl_rails/issues/53並將factory_girl_rails添加到Gemfile – Greg

0

聽起來像是你加入RSpec的你已經創建的Rails項目之後。您需要刪除測試單元發生器參考。請確保您有運行rails g rspec:install和下面是不是在你的config/application.rbhttp://edgeguides.rubyonrails.org/generators.html#customizing-your-workflow):

g.test_framework :test_unit, fixture: true 

當創建一個新的Rails項目,你可以省略測試單元與-T, [--skip-test-unit]標誌。

+0

嘿,如你所說。我跟着它,並再次創造新的,但結果是這樣的 軌G型電影showtime_date:日期showtime_time:時間 調用active_record 創建DB /遷移/ 20130607172805_create_movies.rb 創建應用程序/模型/ movie.rb –

+0

@ AmritdeepDhungana做了你的run rails'rspec:install'?如果是的話,輸出是什麼? –

+0

的出了如下 軌摹rspec的:安裝 創建.rspec 創建規範 創建規範/ spec_helper.rb –

3

嗯,我也遇到過類似的問題4.1。但最終,下面的票證幫助我解決了這個問題。 https://github.com/rspec/rspec-rails/issues/439

總之,您必須確保rspec-rails位於development組中,而不僅位於test組中。隨着這個小小的變化和bundle install,我得到了所有自動生成的規格文件。

+1

或者您可以執行'RAILS_ENV = test rails g model whatever'。 –