2012-06-03 78 views
0

39個測試通過但1個失敗。rspec - 重定向到Group.last出錯記錄

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> 
+0

的Rails 3.2.4有一個奇怪的錯誤有點像 - 如果你正在使用3.2.4你嘗試過3.2.5? –

回答

1

答案是模型(組)按組名命令作爲默認範圍。

的修復只是在測試中使用Group.unscoped.last