【问题标题】:What is the subject of Rspecs "#its" methodRspecs“#its”方法的主题是什么
【发布时间】:2010-04-30 10:53:34
【问题描述】:

当您在 rspec 中使用 its 方法时,如下所示 its(:code) { should eql(0)} '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 国家 API 回复 - 应该是 Api::GetCountryListReply 的一个实例 AdminlwController 国家 API 回复国家列表 - 应该是 Array 的一个实例 - 应该有 2 个项目 - 列表中应该有国家 AdminlwController 国家 API 回复结果状态 - 应该是 Api::SoapStatus 的一个实例 - 应该有一个代码 0 - 应该有一个空的错误数组 AdminlwController 国家 API 回复结果状态码 - 应该为空(失败 - 1) 1) 'AdminlwController 国家 API 回复结果状态代码应为空'中的 NoMethodError <0x40fc4dc>

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    它使用你写its的地方的主题。它不受在代码后面重新定义主题的影响。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 2019-04-19
      • 2010-10-06
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多