【问题标题】:rspec Let() behavior in helper specs帮助规范中的 rspec Let() 行为
【发布时间】:2013-01-06 11:03:34
【问题描述】:

我的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)

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

【问题讨论】:

    标签: ruby-on-rails rspec tdd rspec-rails


    【解决方案1】:

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

    it 'print debug output' do
      puts object.inspect
    end
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-15
        • 2011-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多