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它,但我是一条“关闭”记录。

看起来可能有点缓存/重新加载问题?

我的 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 回答