对块的rspec期望

时间:2015-02-04 10:20:14

标签: ruby rspec

我在测试下面有以下代码:

class MethodCache
    @@methods=Hash.new

    def self.add_method(name, &block)
        @@methods[name]=block
    end

    def self.get_method(name)
        @@methods[name]
    end
end

现在我的规范看起来像这样:

describe MethodCache do
    subject {MethodCache}
    foo_block = ->{ puts "foo"}
    it ".get_method" do
        subject.add_method "foo", &foo_block
        # does not work
        # expect(subject.get_method("foo").to be &foo_block 

        # should syntax works
        subject.get_method("foo").should be foo_block

    end
end

我正试图远离should语法并使用RSpec的expect语法。但是在这种情况下它不起作用。

expect(subject.get_method("foo").to be &foo_block未能说wrong number of arguments。我想这是因为期望块被视为块参数。

expect(subject.get_method("foo").to be foo_block(没有'&')也不起作用。它说,匹配者期望一个价值而不是争论。

我在这里缺少什么?

0 个答案:

没有答案
相关问题