I need to setup a cascading "Given" -- one factory that belongs_to the factory before it ... in plain rspec, I'd create he first factory, then take it's ID and pass it to the next factory.
@widget = Factory(:something)
@otherthing = Factory(:other, :widget_id => @widget.id)
What's the best way to do this in开发者_运维技巧 a step definition? My scenario says: "Given a widget AND a thing", but that creates two steps that don't seem to know anything about the other.
You can use the same approach as RSpec. A step can access instance variables set in a different step.
As Andy says - steps within a scenario can share state with each other through instance variables (because each scenario is executed within a new instance of the World
object).
In your case I'd write something like:
Given /^a widget$/ do
@the_widget = Factory(:something)
end
Given /^a thing$/ do
raise "Must create a widget first!" if @the_widget.nil? # In case the scenario author forgets the widget-creation step
@the_otherthing = Factory(:other, :widget_id => @widget.id)
end
精彩评论