[英]RSpec - How to create helper method available to tests that will automatically embed “it” tests
I am new to ruby/rails/rspec etc. 我是ruby / rails / rspec等的新手。
Using rspec 2.13.1, I want to create a module with a method that can be called from my tests resulting to subsequent calls of the "it" method of the RSpec::Core::ExampleGroup. 我想使用rspec 2.13.1创建一个模块,该模块的方法可以从我的测试中调用,从而导致随后调用RSpec :: Core :: ExampleGroup的“ it”方法。
My module: 我的模块:
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
The code above was added to: 上面的代码已添加到:
spec/support/test_helper.rb
and in my spec_helper.rb, in the RSpec.configure block, I added the following: 在我的spec_helper.rb中的RSpec.configure块中,添加了以下内容:
config.include TestHelper
Now, in a test, I do the following: 现在,在测试中,我将执行以下操作:
describe Foo
context "when invalid" do
invalid_without [:name, :surname]
end
end
Running this, I get: 运行此,我得到:
undefined method `invalid_without' for #<Class:0x007fdaf1821030> (NoMethodError)
Any help appreciated.. 任何帮助表示赞赏。
Use shared example group . 使用共享示例组 。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.