I have a form at the bottom of my users index page for adding additional users.
<%= form_with model: @user, url: user_registration_path do |form| %>
<%= form.text_field :email, id: :email, class: :email %>
<%= form.password_field :password, id: :password, class: :password %>
<%= form.password_field :password_confirmation, id: :password_confirmation, class: :password_confirmation %>
<%= form.submit %>
<% end %>
From trying it out manually, I can see that it works. In the console I can see additional users are added to the db.
But my test fails!
test "additional users can be created" do
post user_registration_path, params: {user: {
email: "new_user123@test.com",
password: "password",
password_confirmation: "password"
}}
follow_redirect!
assert_equal @initial_user_count+1, User.count
end
The user count remains the same.
Expected: 2
Actual: 1
Dropping into the debugger, I see that no additional users have been added. Investigating even further by dropping binding.pry at the top of Users::RegistrationsController#create, I've found that the test doesn't even call #create, whilst it does get called when using the form in the browser. I've also put a binding.pry in a before action in Users::RegistrationsController. Again, the action gets called when using the form in the browser but not in the test. I'm confused!
In both instances I'm posting to user_registration_path with exactly the same parameters but something is preventing the #create method from being called during the test. The email address I'm using in the test is unique -- it does not appear in the fixtures -- and 'password' is a legitimate password.
I've spent too many hours stuck on this. I do not know whether or not the test is working and the code is broken or vice versa. Is there anything obvious that I'm doing wrong. Why isn't post working in the test?
If it helps, here are my user registration routes:
user_registration PATCH / users/registrations#update
PUT / users/registrations#update
DELETE / users/registrations#destroy
POST / users/registrations#create
Aucun commentaire:
Enregistrer un commentaire