User.find_by(email: @user.email) is valid depending which version of Rails you are on; I believe Rails 4.x supports this format. My guess is that your user isn’t valid and thus does not save. Change line 87 to use save!:
before { @user.save! }
--To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/d945fb01-2370-4b03-9460-3b3801ecad74%40googlegroups.com.
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
This is one reason I personally dislike this style of specs combined with AR hooks. It makes more difficult than necessary to troubleshoot.
As @javix pointed out, line 20 has:
it { should be_valid }
At this point the subject is what is defined on line 11:
subject { @user }
The ivar @user is defined in the before block on line 5:
before do
@user = User.new(name: "Example User", email: "us...@example.com",
password: "foobar", password_confirmation: "foobar")
end
However, when the spec on line 91 runs it calls save in the before block on line 87:
before { @user.save }
By using save! it raises an error and confirms that the model was invalid. Here’s the error:
Failure/Error: before { @user.save! }
ActiveRecord::RecordInvalid:
Validation failed: Password confirmation doesn't match Password
Dumping the attributes of @user shows there is a mismatch in the password:
{
password: "foobar",
password_confirmation: "mismatch",
}
This is happening because there is an additional before hook that runs defined on line 87. On a visual inspection, it doesn’t appear that it is initially related because the indenting is out of sync. The end on line 84 really applies to the describe on line 81.
--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/09a1007d-9811-4548-80f9-e5b0f3f33a17%40googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "rspec" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rspec/3j266FPKVy4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/CAKCESdi5aX6_4_8Fguvd0WNA3K%3DrCZy622EoiTHz9G6kK%2BAG%2BA%40mail.gmail.com.
You’ll need to provide more details. Fixing that end worked for me.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/513a7aec-cca3-4f22-9ec7-d4889b4d0b59%40googlegroups.com.
That’s still missing an end:
describe "when password doesn't match confirmation" do
before { @user.password_confirmation = "mismatch" }
it { should_not be_valid }
# <---- No `end`
describe "with a password that's too short" do
before { @user.password = @user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/8da7033b-95a4-4e61-88a8-bb2ea4a94748%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/f2c96d10-026a-4fda-ab6d-9eb586d43824%40googlegroups.com.
AR = “ActiveRecord”
My comment was more simply just a personal style preference. There is no one correct way to write specs.
Personally, I do not like specs which modify variables or helpers defined in different scopes via before blocks. This leads to very confusing, “where was this defined? where was it modified?” questions and debugging sessions.
This is just my 2cents and take it with several grains of salt, but I would consider writing the spec this way: https://gist.github.com/cupakromer/fc5ccd67714c878dd5d8
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/f2c96d10-026a-4fda-ab6d-9eb586d43824%40googlegroups.com.