expect(
find('input[name="commit"]').click
).to change{User.count}.by(1)
When using the change matcher, I had to block expect. In other words, if it is (), the argument is passed, so it seems that you can pass the operation itself as {}. With that in mind, rewrite the source.
expect{
find('input[name="commit"]').click
}.to change{User.count}.by(1)
When I ran it, it worked fine.
There is also a way to write the block as the method name do / end, so I rewrote the source like this and executed it.
expect do
find('input[name="commit"]').click
end.to change{User.count}.by(1)
27: # }.to change{User.count}.by(1)
28: #Experiment
29: # expect(
30: # find('input[name="commit"]').click
31: # ).to change{User.count}.by(1)
=> 32: binding.pry
33: expect do
34: find('input[name="commit"]').click
35: end.to change{User.count}.by(1)
36:
37:
[1] pry(#<RSpec::ExampleGroups::Nested::Nested>)> exit
If you enter the correct information, you can register as a new user and move to the top page.
When new user registration is not possible
If the information is incorrect, the user cannot be newly registered and will return to the new registration page.
Finished in 17.61 seconds (files took 1.73 seconds to load)
2 examples, 0 failures
I confirmed that it works normally even with the method name do / end.
By the way, I checked one more thing. Changed the block of chage to do / end.
expect do
find('input[name="commit"]').click
end.to change do User.count end.by(1)
Failure/Error:
expect do
find('input[name="commit"]').click
end.to change do User.count end.by(1)
SyntaxError:
Block not received by the `change` matcher. Perhaps you want to use `{ ... }` instead of do/end?
I got a syntax error.
Error Block not received by the change
matcher. Perhaps you want to use{...}
instead of do/end?
Translation change
Matcher has not received the block. Perhaps you want to use {...}
instead of do / end?
The result was told to use {}. It seems good to unify with {} without using do / end.