我有一个users_controller.rb文件,其中创建操作定义为: -
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 :(得分:0)
所以你可以用以下方法用rspec和capybara来测试它。对于填充部分,您必须使用页面上的输入元素。
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