0

39 件のテストが成功しましたが、1 件が失敗しました。

Expected response to be a redirect to <http://test.host/groups/11> 
but was a redirect to <http://test.host/groups/12>

新しいレコードを作成してリダイレクトしようとしましshowたが、1 つのレコードが「オフ」になっています。

おそらくキャッシング/リロードの問題のように見えますか?

私のrspecテスト:

describe "POST #create" do

  ...

  context "with valid attributes" do
    it "creates a new group" do
      expect{
        post :create, group: FactoryGirl.attributes_for(:group)
      }.to change(Group,:count).by(1)
    end

    it "redirects to the new group" do
      post :create, group: FactoryGirl.attributes_for(:group)
      response.should redirect_to Group.last
    end
  end

  ...

私のコード:

  # POST /groups
  # POST /groups.xml
  def create
    @group = Group.new(params[:group])

    respond_to do |format|
      if @group.save
        flash[:notice] = 'Group was successfully created.'
        format.html { redirect_to(@group) }
        format.xml  { render :xml => @group, :status => :created, :location => @group }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @group.errors, :status => :unprocessable_entity }
      end
    end
  end

結果:

カウントは変更されます (そのテストはパスします) が、直接の敵は最後のレコードです。つまり、次のようになります。

 Failure/Error: response.should redirect_to Group.last
   Expected response to be a redirect to <http://test.host/groups/11> 
   but was a redirect to <http://test.host/groups/12>
4

1 に答える 1

2

答えは、モデル (グループ) がデフォルトのスコープとしてグループ名による順序を持っているというものでした。

修正はGroup.unscoped.lastテストで使用するだけでした!

于 2012-06-03T21:23:39.763 に答える