Stick this in vendor/plugins to have cucumber steps that create your models easily from factory_girl/machinist/active_record
References to the models are stored, not for the purpose of checking the db (although you could use it for
that), but for enabling easy reference to urls, and for building complex givens which require a bunch of
models collaborating
Now have a look at features/step_definitions/pickle_steps.rb
For capturing english versions of model names you get
CaptureModelGiven /^#{CaptureModel} exists$/ do |model_name| model(model_name).should_not == nil end Then /^I should be at the (.*?) page$/ |page| if page =~ /#{CaptureModel}'s/ url_for(model($1)) else # ... end end
For capturing a field string, you get
CaptureFields
Given /^#{CaptureModel} exists with #{CaptureFields}$/ do |model_name, fields| create_model(model_name, fields) end
Take a look at features/step_definitions/pickle_steps.rb for more examples
This will create a model using the factory name, and optional model label, with the field_string provided. The created model can be later referred to via its name.
For example:
create_model 'a user' # => will create a User create_model 'the user: "1"' # => will create a User, enabling later reference to it with 'user: "1"' create_model 'the user', 'name: "Fred"' # => will create a User with attributes {:name => "Fred"}
If you don’t use Machinist or FactoryGirl, you can still create models, but you must pass in all the fields required to make them valid.
However, if you do use Machinist or FactoryGirl, then just use the factory or blueprint name, and will be super sweet.
This will find a model of the passed class matching the passed field string. The found model can be later referred to by its name.
For example:
find_model('a user', 'name: "Fred'") # => find a user matching those attributes
Refers to a model that has already been created/found (ie. referred to in a scenario)
For example:
create_model('a user') model('the user') # => refers to above user model('a user') # => refers to above user create_model('a car: "herbie"') # => herbie create_model('a car: "batmobile"') # => the batmobile model('the first car') # => herbie model('the 2nd car') # => batmobile model('the car "herbie") # => herbie
model always pulls from the db to get a fresh copy. If you need to access the originally created object for some reason (perhaps it has a one-time key on it that is used in a mailer for example), you can retreive it with created_model