rspec - 如何更改这些简单的“x.should =='val'行

时间:2013-11-18 00:48:36

标签: ruby rspec

对于rspec更改使用expect()。而不是.should,我将如何更改以下内容:

  ...
  describe '#push_state' do
    def process(klass)
      state = OpenStruct.new(:namespace => "ROOT", :scope => :instance, :owner => "ROOT")
      klass.new(state, nil).process
    end 

    it "should push and return all old state info after block" do
      class PushStateHandler1 < Handlers::Base
        def process
          push_state(:namespace => "FOO", :scope => :class, :owner => "BAR") do
            namespace.should == "FOO"
            scope.should == :class
            owner.should == "BAR"
          end 
          namespace.should == "ROOT"
          owner.should == "ROOT"
          scope.should == :instance
        end 
      end 
  ...

我失败的每一种,都可以通过undefined method expect for #<PushStateHandler1:0x000000010a3470>

例如我尝试过:

expect(namespace).to eq "FOO"
expect{(namespace) eq "FOO"}.to be_true

1 个答案:

答案 0 :(得分:0)

expect方法在it块的范围内定义。在这种情况下,您已在该块中定义了一个类,该类中的实例方法以及该实例方法中的块。我不知道指定Ruby作用域规则,但是内部块中的变量不在外部块的范围内解释也就不足为奇了。 should方法仅有效,因为它是在Object上定义的。

所以,为了回答你的问题,我不知道如何从should转换为expect,除非说你以某种方式必须传递并使用外部块的绑定评估您尝试使用的expect个表达式。