Ruby on rails 与shoulda和factory_girl一起测试REST-销毁

Ruby on rails 与shoulda和factory_girl一起测试REST-销毁,ruby-on-rails,unit-testing,shoulda,factory-bot,Ruby On Rails,Unit Testing,Shoulda,Factory Bot,我正在使用shoulda和factory_girl开发REST测试。代码如下 context "on :delete to :destroy" do setup do @controller = NewsArticlesController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @ne

我正在使用shoulda和factory_girl开发REST测试。代码如下

 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
结果我明白了

  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. '
你们能告诉我什么是错误的,我如何修改测试使它们正常工作

UPD:路线看起来不错

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

调用delete时,可能应该使用symbol和post方法:

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

(引用自)

调用delete时,可能应该使用symbol和post方法:

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

(从中引用)

问题在于
应该重定向到
,现在它使用block来计算重定向代码。遗憾的是,无论是thoughtbot wiki还是github的自述都没有反映这一点,并且仍然包含旧的示例

正确的代码是

should_redirect_to "news articles page" { news_articles_path }

其中第一个参数只是用于生成测试名称的文本描述(与旧版本不同,它不是求值的),因此您会得到一个类似“应该重定向到新闻文章页面”的测试名称。

问题在于
应该重定向到
,它现在使用block来计算重定向代码。遗憾的是,无论是thoughtbot wiki还是github的自述都没有反映这一点,并且仍然包含旧的示例

正确的代码是

should_redirect_to "news articles page" { news_articles_path }

其中,第一个参数只是用于生成测试名称的文本描述(与旧版本不同,它不是eval),因此您会得到一个测试名称,如“应该重定向到新闻文章页面”

tkramar解决方案指向正确的方向,但我不得不将代码编写为:

should_redirect_to("news articles page") { news_articles_path }

另请参见tkramar解决方案中的新手册,它指出了正确的方向,但我不得不将代码编写为:

should_redirect_to("news articles page") { news_articles_path }

也可以在

No,assert\u difference上查看新手册,但正如您在提供的日志中所看到的,应该重定向到新闻文章路径存在问题。我想,删除后您不会被重定向到索引?或者你呢?def destroy@news\u article=news article.find(params[:id])@news\u article.destroy response\u to do | format | format.html{redirect_to(news\u articles\u url)}format.xml{head ok}end end end我不知道这是否是正确的语法,但不应该在“应该重定向到新闻文章”路径中-“销毁新的新闻文章“-block?我的意思是,如果您在assert\u difference语句下复制should\u redirect\u to news\u articles\u路径,它是否有效?不,assert\u difference工作正常,但正如您在提供的日志中所看到的,should\u redirect\u to news\u articles\u path存在问题。我认为,删除后您不会重定向到索引?或者你呢?def destroy@news\u article=news article.find(params[:id])@news\u article.destroy response\u to do | format | format.html{redirect_to(news\u articles\u url)}format.xml{head ok}end end end我不知道这是否是正确的语法,但不应该在“应该重定向到新闻文章”路径中-“销毁新新闻文章”-block?我的意思是,如果您在assert\u difference语句下复制should\u redirect\u to news\u articles\u路径,它会起作用吗?