0

Hi i'm working through Hartl. Ive got to the micro posts section in Chapter 10 but I'm generating 9 fails which I don't understand!

Failures:

  1) Hotel 
     Failure/Error: it {should be_valid}
       expected valid? to return true, got false
     # ./spec/models/hotel_spec.rb:19:in `block (2 levels) in <top (required)>'

  2) Hotel user 
     Failure/Error: its(:user) { should == user }
       expected: #<User id: 211, name: "Person 80", email: "person_78@example.com", created_at: "2013-04-26 22:09:22", updated_at: "2013-04-26 22:09:22", password_digest: "$2a$04$ofUpcvsd84qDWTXG131PYedxXdTy2nPzTME/Gf5rc8c0...", remember_token: "wsF59SbMlKni1mqKLhFX0A", admin: false>
            got: nil (using ==)
     # ./spec/models/hotel_spec.rb:17:in `block (2 levels) in <top (required)>'

Here's my hotel_spec.rb file

require 'spec_helper'

describe Hotel do
  let(:user) {FactoryGirl.create(:user)}
  before {@hotel = user.hotels.build(title: "Hilton", room_description: "Biiiiig Room",include_breakfast: "false", price: 566.6, adress:"Smolnaya")}

    subject(@hotel)

    it {should respond_to(:title)}
    it {should respond_to(:room_description)}
    it {should respond_to(:include_breakfast)}
    it {should respond_to(:price)}
    it {should respond_to(:adress)}
    it {should respond_to(:user_id)}
    it {should respond_to(:user)}

    its(:user) { should == user }

    it {should be_valid}

    describe "accesible attributes" do
      it "should not allow access to user_id" do
        expect do
          Hotel.new(user_id: user.id)
        end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
      end
    end

    describe "when user_id is not present" do
      before {@hotel.user_id = nil}
      it {should_not be_valid}
    end

end

my hotel.rb file

class Hotel < ActiveRecord::Base
  attr_accessible :adress, :include_breakfast, :price, :room_description, :title
  belongs_to :user

  validates :user_id, presence: true
end

where I made a mistake?Any ideas and solutions are much appreciated. Thanks.

2 Answers2

2
subject { @hotel }

Braces, not parentheses.
subject expects a code block (i.e. similar to do ... end), not arguments.

A little off-topic, but it may be helpful to follow the convention of including white space within the braces.

Community
  • 1
  • 1
Sun
  • 777
  • 5
  • 13
1

@Sunxperous answer seems to be correct - I would recommend you use the thoughtbot shoulda gem to make your tests even simpler. Also use FactoryGirl to create your hotel model. With the gem you rspec could look like

require 'spec_helper'

describe Hotel do

  let(:user) { FactoryGirl.create :user }
  subject(:hotel) { FactoryGirl.create :hotel, user: user }

  it { should be_valid } # a valid factory is very important

  it { should_not allow_mass_assignment_of :user_id }
  it { should validates_presence_of :user }
  it { should belong_to :user }

  it {should respond_to(:title)}
  it {should respond_to(:room_description)}
  it {should respond_to(:include_breakfast)}
  it {should respond_to(:price)}
  it {should respond_to(:adress)}
  it {should respond_to(:user_id)}
  it {should respond_to(:user)}

end
roo
  • 7,106
  • 8
  • 39
  • 45