这是通过测试:
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变量?
答案 0 :(得分:1)
使用.attributes
方法获取用户属性的哈希值,因此您可以发送它们而不是实际的用户对象。
test "should create user" do
assert_difference('User.count') do
post :create, user: @user.attributes
end