【问题标题】:testing REST with shoulda and factory_girl - destroy使用 shoulda 和 factory_girl 测试 REST - 销毁
【发布时间】:2009-07-29 08:20:53
【问题描述】:

我正在使用 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"}

【问题讨论】:

    标签: ruby-on-rails unit-testing shoulda factory-bot


    【解决方案1】:

    问题在于should_redirect_to 现在使用块来评估重定向代码。遗憾的是,thoughtbot wiki 和 github 上的自述文件都没有反映这一点,并且仍然包含旧示例。

    正确的代码是

    should_redirect_to "news articles page" { news_articles_path }
    

    其中第一个参数只是用于生成测试名称的文本描述(它不像旧版本那样被评估),因此您会得到一个测试名称,例如 '应该重定向到新闻文章页面'

    【讨论】:

      【解决方案2】:

      也许你应该在调用 delete 时使用符号和 post 方法:

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

      (引用自http://api.rubyonrails.org/classes/ActiveSupport/Testing/Assertions.html#M001427

      【讨论】:

      • 不,assert_difference 工作正常,但正如您在提供的日志中看到的那样 - should_redirect_to news_articles_path 存在问题
      • 我想,你删除后不会重定向到索引吗?你呢?
      • 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
      • 我不知道这是否是正确的语法,但“should_redirect_to news_articles_path”不应该在应该-“destroy new NewsArticle”-块中吗?
      • 我的意思是,如果你将assert_difference语句下的should_redirect_to news_articles_path复制过来,是否有效?
      【解决方案3】:

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

      should_redirect_to("news articles page") { news_articles_path }
      

      另请参阅http://dev.thoughtbot.com/shoulda/classes/Shoulda/ActionController/Macros.html#M000015 的新手册

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-05
        • 1970-01-01
        相关资源
        最近更新 更多