1

为什么这行不通?

有人在说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
4

2 回答 2

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 个区别对待。

于 2013-06-06T11:33:09.257 回答
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
于 2013-06-07T13:02:13.440 回答