views:

67

answers:

1

When you use the its method in rspec like follows its(:code) { should eql(0)} what is 'its' referring to.

I have the following spec that works fine

describe AdminlwController do

  shared_examples_for "valid status" do
    it { should be_an_instance_of(Api::SoapStatus) }
    it "should have a code of 0" do
      subject.code.should eql(0)
    end
    it "should have an empty errors array" do
      subject.errors.should be_an(Array)
      subject.errors.should be_empty
    end
    #its(:code) { should eql(0)}
  end

  describe "Countries API Reply" do 
    before :each do
      co1 = Factory(:country)
      co2 = Factory(:country)
      @result = invoke :GetCountryList, "empty_auth"
    end

    subject { @result } 
    it { should be_an_instance_of(Api::GetCountryListReply) }

    describe "Country List" do
      subject {@result.country_list}
      it { should be_an_instance_of(Array) }
      it { should have(2).items }
      it "should have countries in the list" do
        subject.each {|c| c.should be_an_instance_of(Api::Country)}
      end
    end

    describe "result status" do
      subject { @result.status }
      it_should_behave_like "valid status"
    end
  end

However if I then uncomment the line with its(:code) then I get the following output

AdminlwController Countries API Reply
- should be an instance of Api::GetCountryListReply

AdminlwController Countries API Reply Country List
- should be an instance of Array
- should have 2 items
- should have countries in the list

AdminlwController Countries API Reply result status
- should be an instance of Api::SoapStatus
- should have a code of 0
- should have an empty errors array

AdminlwController Countries API Reply result status code
- should be empty (FAILED - 1)

1)
NoMethodError in 'AdminlwController Countries API Reply result status code should be empty'
undefined method code for <AdminlwController:0x40fc4dc>
/Users/steveweet/romad_current/romad/spec/controllers/adminlw_controller_spec.rb:29:

Finished in 0.741599 seconds

8 examples, 1 failure

It seems as if "its" is referring to the subject of the whole test, namely AdminLwController rather than the current subject.

Am I doing something wrong or is this an Rspec oddity?

A: 

It uses the subject at the point where you write its. It is not affected by a redefinition of the subject further down the code.

henning-koch