I am learning Rails with the Rails Tutorial. The author actively teaches selective bits of RSpec. In exercises after each chapter, you have the opportunity to learn to write pithier code. I have chosen to do those exercises. But, the problem is, in the following chapters, he treats the reader as if they didn't, so the pithier code you wrote lends itself to challenges when you don't know how to adapt the new 'unpithy' code he writes to it's 'pithier' counterpart.
In the pithy version, the beginning of the file lends itself to less repetition, and looks as such:
require 'spec_helper'
describe User do
before { @user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar") }
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation )}
it { should be_valid }
.
.
.
end
So, then our tests look like so:
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid }
end
When attempting to create a test for whether or not the @user.password
and @user.password_confirmation
are present, I am looking to continue this RSpec format.
The authors version looks as such:
describe "when email is not present" do
before do
@user= User.new(name: "Example User", email: "user@example.com",
password: " ", password_confirmation = " ")
end
it { should_not be_valid }
end
My attempt at it (since the author reverts to the method of doing things without the exercise of pithier RSpec code) would be as such:
describe "when password is not present" do
before { @user.password = " ", @user.password_confirmation = " " }
it { should_not be_valid }
end
Is this the proper way to modify multiple hash values using a before in this context?
Furthermore, I am struggling to locate answers for my RSpec questions. Does anyone have a solid routine for finding these kinds of answers?