views:

991

answers:

3

Hi, i'm developing test for REST using shoulda and factory_girl. Code below

 context "on :delete to :destroy" do
    setup do
      @controller = NewsArticlesController.new
      @request = ActionController::TestRequest.new
      @response = ActionController::TestResponse.new

      @news_article =  Factory.create(:news_article)

    end

    should "destroy new NewsArticle" do
      assert_difference('NewsArticle.count', -1) do
        delete :destroy, :id => @news_article.id
      end
    end

    should_redirect_to news_articles_path
  end

as a result i see

  1) Error:
test: on :delete to :destroy should redirect to index. (NewsArticlesControllerTest):
ArgumentError: block not supplied
    c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/action_controller/macros.rb:201:in `instance_eval'
    c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/action_controller/macros.rb:201:in `__bind_1248853182_16800
0'
    c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
    c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: on :delete to :destroy should redirect to index. '

Could you tell me plz - whats wrong and how i can modify test to make them work right?

UPD: routes looks fine

news_articles GET    /news(.:format)                    {:controller=>"news_articles", :action=>"index"}
+1  A: 

Maybe you should use a symbol and post method when calling delete:

 assert_difference 'Article.count', -1 do
    post :delete, :id => ...
  end

(referenced from http://api.rubyonrails.org/classes/ActiveSupport/Testing/Assertions.html#M001427)

Lichtamberg
No, assert_difference works fine, but as you can see in provided logs - there are problem with should_redirect_to news_articles_path
Alexey Poimtsev
Could you show me your controller-file?
Lichtamberg
I think, you don't get redirected to the index after deleting? or do you?
Lichtamberg
def destroy @news_article = NewsArticle.find(params[:id]) @news_article.destroy respond_to do |format| format.html { redirect_to(news_articles_url) } format.xml { head :ok } end end
Alexey Poimtsev
I dont know if this is the right syntax, but shouldnt "should_redirect_to news_articles_path" be in the should-"destroy new NewsArticle"-block?
Lichtamberg
I mean, does it work if you copy the should_redirect_to news_articles_path under the assert_difference statement?
Lichtamberg
huh .... working :) but i've changed should_redirect_to to assert_redirected_to news_articles_path
Alexey Poimtsev
+5  A: 

The problem is with should_redirect_to which now uses block to evaluate the redirect code. Sadly, neither thoughtbot wiki, nor the readme at github reflect this and still contain the old examples.

The correct code is

should_redirect_to "news articles page" { news_articles_path }

where the first argument is just a textual description (it is not eval'd as with the older version) used to generate a test name, so you get a test name like 'should redirect to news articles page'

+1  A: 

tkramar solution points in the right direction, but i've had to write the code as:

should_redirect_to("news articles page") { news_articles_path }

Also see the new manual at http://dev.thoughtbot.com/shoulda/classes/Shoulda/ActionController/Macros.html#M000015

Sytse Sijbrandij