2011-04-13 48 views
1

我想在我的客戶控制器上運行一些測試。當我手動測試控制器時,所有工作都正常,但是當我在其上編寫集成測試時,出現錯誤。這裏是我的測試代碼:assert_redirected_to在Rails測試

context "non-empty Customer model" do 
    setup do 
    @customer = Customer.first || Customer.create(:name => "John", :address => "123 Street Cool", :telephone => "01484349361", :email => "[email protected]") 
    end 

    should "be able to create" do 
    get "/customers/new" 
    assert_response :success 
    post "/customers/create", :post => @customer 
    # assert_response :success 
    assert_redirected_to "/customers/list" 
    end 

而且我得到的錯誤是在assert_redirected_to線和它說:

預期塊返回真值。

這裏是新/創建操作我的控制器代碼:

def new 
    @customer = Customer.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @customer } 
    end 
    end 

    def create 
    # Instantiate a new object using form params 
    @customer = Customer.new(params[:customer]) 
    # Save the object 
    if @customer.save 
     # If save succeeds, redirect to the list action 
     flash[:notice] = "Customer created." 
     redirect_to(:action => 'list') 
    else 
     # If save fails, redisplay the form so user can fix problems 
     render('new') 
    end 
    end 

我將不勝感激地讓工作測試任何幫助。

謝謝。

回答

2

看起來你發送的錯誤信息是params。

should "be able to create" do 
    get "/customers/new" 
    assert_response :success 
    post "/customers/create", :customer => @customer.attributes 
    assert_redirected_to "/customers/list" 
end