0

我有以下内容application_helper_spec.rb

shared_examples_for "some shared process" do

    it "displays the value defined by Let" do
        puts object.inspect # This should output the value defined by Let
    end

end

describe ApplicationHelper do

    describe "a_helper_methond" do

        describe "some behaviour" do
            let(:object) { "foo" }
            puts object.inspect # This should output the value defined by Let
            it_should_behave_like "some shared process"
        end

    end

end

但是,当我运行它时,我在两个 put 上都出现了两个错误:

undefined local variable or method `object' for #<Class:0x007f951a3a7b20> (NameError)

为什么?我的模型中有完全相同的代码,并且请求规范运行良好,但在辅助规范中却没有。

4

2 回答 2

4

let调用在您的规范将被执行的上下文中定义了一个方法。但是,您的puts陈述超出了该范围。您需要将其包装在一个it块中。

it 'print debug output' do
  puts object.inspect
end
于 2013-01-06T11:31:30.103 回答
0

我还通过将 Let 命令放在 it_behaves_like 块中进一步改进了这一点

于 2013-01-06T19:45:15.890 回答