RSpec/Shoulda: "should be_valid" fails, "contact.should be_valid" passes

rspec, ruby, ruby-on-rails, validation

Solution

You could use `subject`

context "no firstname present" do
  let(:contact) { build :contact, firstname: 'Mickey', lastname: '', companyname: '' }
  subject { contact }
  it { should be_valid }
end

Edit

I just discovered `its` [link] which is a great way to test the `subject`'s attributes or test messages sent to it

Problem

I have the following spec: ``` context "no firstname present" do let(:contact) { build :contact, firstname: 'Mickey', lastname: '', companyname: '' } it "should be valid" do should be_valid # Does not work contact.should be_valid # Works end end ``` Why does `should be_valid` fails, but `contact.should be_valid` passes? Within the `it` block, `should be_valid` should access `contact` anyway?! Thanks for clarification.

Original source