2010-12-07 13 views
2

我正在嘗試執行一些集成測試以查看文章是否得到更新。這裏是我的集成測試:與Ruby集成測試和assigns關鍵字

test "update existing article" do 

    # create an article 
    @article = Article.new 
    @article.title = "title" 
    @article.abstract = "abstract" 
    @article.description = "description" 

    if @article.save 

     post "/articles/edit", :id => @article.id 

     assert assigns(:article) 

    end 

    end 

這裏是我的articles_controller實現:

def edit 

    @article = Article.find(params[:id]) 

    respond_to do |format| 
     format.html 
     format.xml { render :xml => @article } 
    end 

    end 

在測試的最後一行約受讓人失敗。從我理解的分配(:文章)意味着變量@article將被填充。

回答

2

看看這一行:

post "/articles/edit", :id => @article.id 

的問題是,編輯操作需要GET,而不是一個POST,所以它可能永遠不會獲取調用。嘗試:

get edit_article_path, :id => @article.id 

(注意,如果你正在運行一個控制器測試,最好使用符號的動作名稱。)

+0

使用GET:編輯我收到以下錯誤:1)錯誤: test_update_existing_article(ArticleFlowsTest): ArgumentError:錯誤的參數(期望的URI對象或URI字符串) /test/integration/adding_article_flows_test.rb:30:in`test_update_existing_article' – azamsharp 2010-12-07 16:33:19