2011-02-01 37 views
2

我在Rails 3測試中遇到了一些麻煩。我目前正在將Rails2應用升級到Rails3。我正在使用shoulda進行測試。在我的功能測試,我與早該測試,這應該是GET應該成功應該在Rails 3測試:應該respond_with:成功

context "GET to :blame" do 
    should "mark a song as blamed" do 
    get :blame, :id => @song.id 
    assert_equal Blame.count, 1 
    get :blame, :id => @song.id 
    assert_equal Blame.count, 2 
    end 
    should respond_with :success 
end 

應對在未來,同時執行與耙測試功能測試,我發現了以下錯誤最後一行:泛函:

1) Error: 
test: a visitor GET to :blame should respond with 200. (SongsControllerTest): 
NoMethodError: undefined method `response_code' for nil:NilClass 
    /Users/23tux/.rvm/gems/[email protected]/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing' 
    /Users/23tux/.rvm/gems/[email protected]/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:57:in `response_code' 
    /Users/23tux/.rvm/gems/[email protected]/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:48:in `correct_status_code?' 
    /Users/23tux/.rvm/gems/[email protected]/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:30:in `matches?' 
    /Users/23tux/.rvm/gems/[email protected]/gems/shoulda-2.11.3/lib/shoulda/assertions.rb:53:in `assert_accepts' 
    /Users/23tux/.rvm/gems/[email protected]/gems/shoulda-2.11.3/lib/shoulda/context.rb:324:in `block in should' 
    /Users/23tux/.rvm/gems/[email protected]/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `call' 
    /Users/23tux/.rvm/gems/[email protected]/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash' 

我使用Ruby 1.9.2,Rails 3.0.3和Shoulda 2.11.3。 希望有人能幫助我。

THX, TUX

回答

2

兩個應塊單獨運行,不請求將用於respond_with因此誤差進行。您需要在設置塊中提出請求,例如:

context "GET to :blame" do 
    setup do 
    get :blame, :id => @song.id 
    end 
    should "mark a song as blamed" do 
    assert_equal Blame.count, 1 
    end 
    should respond_with :success 
end