2016-02-12 68 views
0

我在其中創建操作定義爲users_controller.rb文件: -在rails RESTfull控制器中測試創建動作的正確方法是什麼?

def create 
    @user = User.new(user_params) 
    if current_user.g_admin? 
    @user.role = 'c_admin' 
    elsif current_user.c_admin? 
    @user.role = 'c_user' 
    end 
    @user.admin_provisioned = true 
    authorize @user 
    respond_to do |format| 
    if @user.save 
     format.html { redirect_to authenticated_root_url, notice: 'User was successfully created and email has been sent.' } 
     format.json { render :show, status: :created, location: @user } 
    else 
     format.html { render :new } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
    end 
    end 
end 

現在,我想編寫測試這種方法反映了該方法的所有行爲。我對測試完全陌生,所以編寫測試的詳細和標準方式將被接受爲答案。 rspec的版本是3.4。快樂的編碼。

回答

0

所以你可以用rspec和水豚來測試這個方法。對於填充部分,您必須使用您的頁面上的輸入元素。

describe "let an guest create a user account" do 
    fill_in :user_object, with: "value" 

    click_button "Save User" 

    expect(page).to have_content("User was successfully created and email has been sent.") 
end 


describe "let an guest try to create an account with invalid data" do 
    fill_in :user_object, with: "invalid value" 

    click_button "Save User" 

    expect(page).to_not have_content("User was successfully created and email has been sent.") 
    expect(page).to have_content("content from the :new page") 
end 
相關問題