In a bunch of rspec rails unit specifications I do something like:
在一堆rspec rails单元规格中,我做了类似的事情:
describe Foo do
[:bar, :baz].each do |a|
it "should have many #{a}" do
Foo.should have_many(a)
end
end
end
For cleaner code I'd rather do something like:
为了更清洁的代码,我宁愿做类似的事情:
describe Foo do
spec_has_many Foo, :bar, :baz
end
So how do I write a helper method like spec_has_many()
for inserting DSL code like rspec's it()
method? If it were for an ordinary instance method I'd do something like:
那么如何编写像spec_has_many()这样的辅助方法来插入DSL代码,如rspec的it()方法?如果它是一个普通的实例方法,我会做类似的事情:
def spec_has_many(model, *args)
args.each do |a|
define_method("it_should_have_many_#{a}") do
model.should have_many(a)
end
end
end
What would be the equivalent for defining rspec examples?
什么是定义rspec示例的等价物?
1 个解决方案
#1
9
Ok, this took some messing around, but I think I got it working. It's a bit of metaprogramming hackery, and I personally would just use the first thing you described, but it's what you wanted :P
好吧,这需要一些麻烦,但我认为我得到了它的工作。这是一个元编程的hackery,我个人会使用你描述的第一件事,但这是你想要的:P
module ExampleMacros
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
# This will be available as a "Class Macro" in the included class
def should_have_many(*args)
args.each do |a|
# Runs the 'it' block in the context of the current instance
instance_eval do
# This is just normal RSpec code at this point
it "should have_many #{a.to_s}" do
subject.should have_many(a)
end
end
end
end
end
end
describe Foo do
# Include the module which will define the should_have_many method
# Can be done automatically in RSpec configuration (see below)
include ExampleMacros
# This may or may not be required, but the should_have_many method expects
# subject to be defined (which it is by default, but this just makes sure
# it's what we expect)
subject { Foo }
# And off we go. Note that you don't need to pass it a model
should_have_many :a, :b
end
My specs fail because Foo doesn't have a has_many?
method, but both tests run, so it should work.
我的规格失败了,因为Foo没有has_many?方法,但两个测试运行,所以它应该工作。
You can define (and rename) the ExampleMacros module in your spec_helper.rb
file and it will be available for inclusion. You want to call include ExampleMacros
in your describe
blocks (and not any others).
您可以在spec_helper.rb文件中定义(并重命名)ExampleMacros模块,它可以包含在内。您想在描述块(而不是其他任何块)中调用包含ExampleMacros。
To make all of your specs include the module automatically, configure RSpec like so:
要使所有规范自动包含模块,请像这样配置RSpec:
# RSpec 2.0.0
RSpec.configure do |c|
c.include ExampleMacros
end
#1
9
Ok, this took some messing around, but I think I got it working. It's a bit of metaprogramming hackery, and I personally would just use the first thing you described, but it's what you wanted :P
好吧,这需要一些麻烦,但我认为我得到了它的工作。这是一个元编程的hackery,我个人会使用你描述的第一件事,但这是你想要的:P
module ExampleMacros
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
# This will be available as a "Class Macro" in the included class
def should_have_many(*args)
args.each do |a|
# Runs the 'it' block in the context of the current instance
instance_eval do
# This is just normal RSpec code at this point
it "should have_many #{a.to_s}" do
subject.should have_many(a)
end
end
end
end
end
end
describe Foo do
# Include the module which will define the should_have_many method
# Can be done automatically in RSpec configuration (see below)
include ExampleMacros
# This may or may not be required, but the should_have_many method expects
# subject to be defined (which it is by default, but this just makes sure
# it's what we expect)
subject { Foo }
# And off we go. Note that you don't need to pass it a model
should_have_many :a, :b
end
My specs fail because Foo doesn't have a has_many?
method, but both tests run, so it should work.
我的规格失败了,因为Foo没有has_many?方法,但两个测试运行,所以它应该工作。
You can define (and rename) the ExampleMacros module in your spec_helper.rb
file and it will be available for inclusion. You want to call include ExampleMacros
in your describe
blocks (and not any others).
您可以在spec_helper.rb文件中定义(并重命名)ExampleMacros模块,它可以包含在内。您想在描述块(而不是其他任何块)中调用包含ExampleMacros。
To make all of your specs include the module automatically, configure RSpec like so:
要使所有规范自动包含模块,请像这样配置RSpec:
# RSpec 2.0.0
RSpec.configure do |c|
c.include ExampleMacros
end