0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unittestでDB更新結果のassertする場合、reloadを忘れずに

Posted at

更新は別インスタンスで行われてるからだ。言われてみれば確かにそうだ。

controller/lists_controller.rb
def stock
    return redirect_to '/404.html' unless request.xhr?
    @list = List.find(params[:id])

    if @list.is_stocked?
      @list.is_stocked = false
    else
      @list.is_stocked = true
    end

    respond_to do |format|
      if @list.save
        format.js { @list }
      else
        format.js { render :action => "alert/error" }
      end
    end
  end

これだとテストに失敗する

functional/lists_controller_test.rb
test "is_stockedを更新する" do
    xhr :get, :stock, :id => @stock_flag_is_true.id
    assert_equal false, @stock_flag_is_true.is_stocked
    assert_response :success
end

別インスタンスで更新した内容をインスタンスに反映させるため、 reload を追加しなければいけない

functional/lists_controller_test.rb
test "is_stockedを更新する" do
    xhr :get, :stock, :id => @stock_flag_is_true.id
    assert_equal false, @stock_flag_is_true.reload.is_stocked
    assert_response :success
end

同じようにハマってる方がいた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?