2010-04-30 44 views
0

當您在rspec中使用其方法時,如下所示its(:code) { should eql(0)}「它」指的是什麼。Rspecs「#its」方法的主題是什麼

我有以下的規範工作正常

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 

但是,如果我再取消註釋與its(:code)那麼該行我得到以下輸出

 
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 

看來,如果「它」是指到整個測試的主題,即AdminLwController而不是當前主題。

我做錯了什麼或者這是一個Rspec奇怪嗎?

回答

0

在寫its的地方使用該主題。它不受對代碼進一步重新定義主題的影響。