我是ruby / rails / rspec等的新手。
使用rspec 2.13.1,我想创建一个带有方法的模块,该方法可以从我的测试中调用,从而导致后续调用RSpec :: Core :: ExampleGroup的“it”方法。
我的模块:
require 'spec_helper'
module TestHelper
def invalid_without(symbols)
symbols = symbols.is_a?(Array) ? symbols : [symbols]
symbols.each do |symbol|
it "should not be valid without #{symbol.to_s.humanize}" do
# Gonna nullify the subject's 'symbol' attribute here
# and expect to have error on it
end
end
end
end
上面的代码已添加到:
spec/support/test_helper.rb
在我的spec_helper.rb中,在RSpec.configure块中,我添加了以下内容:
config.include TestHelper
现在,在测试中,我执行以下操作:
describe Foo
context "when invalid" do
invalid_without [:name, :surname]
end
end
运行这个,我得到:
undefined method `invalid_without' for #<Class:0x007fdaf1821030> (NoMethodError)
任何帮助表示赞赏..
答案 0 :(得分:4)
shared_examples_for "a valid array" do |symbols|
symbols = symbols.is_a?(Array) ? symbols : [symbols]
symbols.each do |symbol|
it "should not be valid without #{symbol.to_s.humanize}" do
# Gonna nullify the subject's 'symbol' attribute here
# and expect to have error on it
end
end
end
describe Foo do
it_should_behave_like "a valid array", [:name, :surname]
end