Rails集成测试 - 填写表单

时间:2013-06-06 11:14:27

标签: ruby-on-rails integration-testing

为什么这不起作用?

有些人在谈论webrat,但Rails不应该直接处理表单注册吗?

以下测试不会触发User#Create操作,我想知道原因。似乎什么也没发生。

test "should not sign up more than 5 users" do
  get "signup"
  assert_response :success
  assert_difference('User.count',5) do
    for i in 0..10 do
      puts "Signup user"
      post_via_redirect "/signup",
                        :first_name => "Petter",
                        :last_name => "Smart",
                        :email => "petter.smart<%=i%>@prayalot.com",
                        :password => '123456',
                        :password_confirmation => '123456'
      assert_equal '/sentmail', path
    end
  end
end

2 个答案:

答案 0 :(得分:0)

好吧,很多问题:

post_via_redirect "/signup",

既然你提到你有#create,这行应该发布到#create。 “/ signup”适用于#new。

:email => "petter.smart<%=i%>@prayalot.com",

插值应为"petter.smart#{i}@prayalot.com"

assert_equal '/sentmail', path

我不知道你的逻辑是怎样的,但如果你的代码每天不允许超过5次注册,那么5之后的用户不应该收到邮件,而是他们可能会看到“今天不再有”的通知,至少他们应该与前5个区别对待。

答案 1 :(得分:0)

这就像一个魅力! :-)感谢Billy Chan的建议。

test "should not sign up more than 10 users from same IP" do
    get "signup"
    assert_response :success
    assert_difference('User.count',10) do
        for i in 1..10 do
            post "users", :user => {
            :first_name => "Petter",
            :last_name => "Smart",
            :email =>  "petter.smart#{i}@prayalot.com",
            :password => '123456',
            :password_confirmation => '123456'}
            assert_redirected_to sentmail_path
        end

        for i in 11..20 do
            post "users", :user => {
            :first_name => "Petter",
            :last_name => "Smart",
            :email =>  "petter.smart#{i}@prayalot.com",
            :password => '123456',
            :password_confirmation => '123456'}
            assert_redirected_to error_path
        end
    end
end