0

这是通过的测试:

test "should create user" do
        assert_difference('User.count') do
        post :create, user: { name: "John Doe", 
                              email: "example@example.com",
                              password: "foobar3",
                              password_confirmation: "foobar3" }
end

这是失败的测试:

def setup
  @user = User.new(name: "John Doe", 
               email: "example@example.com",
               password: "foobar3",
               password_confirmation: "foobar3")
end


test "should create user" do
        assert_difference('User.count') do
        post :create, user: @user 
end

为什么第二次测试失败?以及如何使它使用@user 变量?

4

1 回答 1

1

使用该.attributes方法仅获取用户属性的散列,因此您发送这些而不是实际的用户对象。

test "should create user" do
    assert_difference('User.count') do
    post :create, user: @user.attributes
end
于 2012-06-19T13:49:10.443 回答