2012-02-08 63 views
1

我剛剛升級到Ruby 1.9.3-p0的Rails 3.2.1,我正在使用Machinist 2.0。在更新大型項目之前,所有測試都通過了。我「米遇到的問題是,當我在我的RSpec的測試中創建一個‘讓’調用中的藍圖,然後參考它在做之前塊。rails 3.2和機械師問題

let (:new_post) {Post.make!} 

    before do 
    Post.stub!(:new).and_return(new_post) 
    end 

這用來工作的,現在我得到的以下錯誤:

1) PostsController GET index assigns all posts as @posts 
    Failure/Error: let (:new_post) {Post.make!} 
    NoMethodError: 
     undefined method `title=' for nil:NilClass 
    # ./spec/support/blueprints.rb:22:in `block in <top (required)>' 
    # ./spec/controllers/posts_controller_spec.rb:37:in `block (2 levels) in <top (required)>' 
    # ./spec/controllers/posts_controller_spec.rb:40:in `block (2 levels) in <top (required)>' 

這裏是我的藍圖:

require 'machinist/active_record' 
Post.blueprint do 
    title {"Post"} 
    body {"hello world"} 
end 

現在我的解決辦法是使用之前做塊中的實例變量來創建它們,但它會是不錯的使用「讓'呼籲,因爲它的關鍵ps我的rspec測試更清潔。

回答

0

有趣的是,我只是遇到了同樣的問題,雖然我在Rails 3.2.1,Machinist 2.0和ruby 1.9.2-p290上。我認爲在執行Post.stub(:new)存根方法和機械師方法make之間存在衝突,但我沒有深入到代碼中。

最好的解決方案我已經出來是:

before do 
    new_post 
    Post.stub!(:new).and_return(new_post) 
    end 

這將初始化設(因爲咱們是懶加載中的RSpec),它得到存根方法之前。這很冒險,但至少你(和我)可以保留let語句。

+0

我升級到Rails 3.2.1並有同樣的問題,但現在看起來這是一個很好的解決方案。 – map7 2012-02-22 00:15:00

+0

很高興爲你效勞。我會嘗試查看代碼並查看哪個項目需要通知該問題。 – poetmountain 2012-02-22 02:21:26