title: rSpec Testing Rails using Shoulda matchers
body: <p>I have been starting to learn testing in my Rails app and am using rSpec and Shoulda.</p>
  
  <p>I have the following test which works:</p>
  
  <pre><code>it { should respond_to(:park_name) }
  </code></pre>
  
  <p>However, what I don't understand is, what is this being run on? Is this being run on the Model itself or an instance of the model and if it's an instance of the Model then is it automatically using my Factory Girl factory?</p>
  
  <p>Any simple explanations on what is actually occurring here?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Ok, So I have this:</p>
  
  <pre><code>  describe 'validations' do
  
      subject { FactoryGirl.build(:coaster) }
  
      it { should validate_presence_of(:name) }
      it { should validate_presence_of(:speed) }
      it { should validate_presence_of(:height) }
  
    end
  </code></pre>
  
  <p>But the tests are failing. Any ideas?</p>
  
  <p>Coaster.rb:</p>
  
  <pre><code>class Coaster &lt; ActiveRecord::Base
  
        extend FriendlyId
        friendly_id :slug, use: :slugged
  
        belongs_to :park
        belongs_to :manufacturer
  
        attr_accessible :name,
                        :height,
                        :speed,
                        :length,
                        :inversions,
                        :material,
                        :lat,
                        :lng,
                        :park_id,
                        :notes,
                        :manufacturer_id,
                        :style,
                        :covering,
                        :ride_style,
                        :model,
                        :layout,
                        :dates_ridden,
                        :times_ridden,
                        :order,
                        :on_ride_photo
  
  
  
        scope :by_name_asc, lambda {
          order("name ASC")
        }
  
        scope :made_from, lambda { |material|
          where("material = ?", material)
        }
  
        scope :wooden, lambda {
          made_from "wood"
        }
  
        scope :steel, lambda {
          made_from "steel"
        }
  
  
  
        delegate :name, :location_1, :location_2, :location_3, :location_4,
          to: :park,
          allow_nil: true,
          prefix: true
  
        delegate :name, :url,
          to: :manufacturer,
          prefix: true
  
  
  
        validates :name,
          :presence =&gt; true
  
        validates :height,
          allow_nil: true,
          numericality: {greater_than: 0}
  
        validates :speed,
          allow_nil: true,
          numericality: {greater_than: 0}
  
        validates :length,
          allow_nil: true,
          numericality: {greater_than: 0}
  </code></pre>
  
  <p>Test Results:</p>
  
  <blockquote>
    <p>1) Coaster validations should require speed to be set
         Failure/Error: it { should validate_presence_of(:speed) }
           Expected errors to include "can't be blank" when speed is set to nil, got no errors
         # ./spec/models/coaster_spec.rb:75:in `block (3 levels) in '</p>
    
    <p>2) Coaster validations should require height to be set
         Failure/Error: it { should validate_presence_of(:height) }
           Expected errors to include "can't be blank" when height is set to nil, got no errors
         # ./spec/models/coaster_spec.rb:76:in `block (3 levels) in '</p>
  </blockquote>
  
  <p>SIMILAR QUESTION:</p>
  
  <p>I have this test:</p>
  
  <pre><code>describe 'methods' do
  
      subject { FactoryGirl.build(:coaster) }
  
      it "should return a formatted string of coaster name at park name" do
          name_and_park.should eq('Nemesis at Alton Towers')
      end
  
    end
  </code></pre>
  
  <p>Coaster.rb:</p>
  
  <pre><code>  def name_and_park
      [name, park.name].join (' at ')
    end
  </code></pre>
  
  <p>Error when running the test:</p>
  
  <blockquote>
    <p>2) Coaster methods should return a formatted string of coaster name at
    park name
         Failure/Error: name_and_park.should eq('Nemesis at Alton Towers')
         NameError:
           undefined local variable or method <code>name_and_park' for #&lt;RSpec::Core::ExampleGroup::Nested_1::Nested_6:0x007f84f4161798&gt;
         # ./spec/models/coaster_spec.rb:111:in</code>block (3 levels) in '</p>
  </blockquote>
  
  <p>It says name_and_park cannot be called but surely that method should be being called on the instance of Coaster that is being made in the subject line? No?</p>