# If you want to DRY your step definitions and forget about resources_steps.rb file, # you can include (once and for all!) scaffolded_resources_steps.rb in your step definitions. # features/step_definitions/products_steps.rb # Given Given /^I have created (\d+) Product(?:s)?$/ do |n| @products ||= [] n.to_i.times do @products << FactoryGirl.create(:product, owner_id: @current_user.id) end end Given /^somebody else has created (\d+) Product(?:s)?$/ do |n| # @user doesn't need to be stable and can be generated often. # On the contrary, @current_user must be stable. # see `Given I have a valid authentication token` @user ||= FactoryGirl.create(:user) @products ||= [] n.to_i.times do @products << FactoryGirl.create(:product, owner_id: @user.id) end end # When When /^I send a (\w+) request to "(.*?)" \(Product\)$/ do |method, path| path = Mustache.render(path, {product_id: @products.last.id}) send(method.downcase, path) end When /^I send a (\w+) request to "(.*?)" with the following \(Product\):$/ do |method, path, body| path = Mustache.render(path, {product_id: @products.last.id}) send(method.downcase, path, body) end # Then Then /^the response body should be a JSON representation of the( created)? Product \(Ember Data conventions\)$/ do |product_was_created| steps %{ Then the response body should fulfill the Ember Data expectations for Product } if product_was_created puts "Internal index was updated after Product creation." @products ||= [] @products << Product.last end json = JSON.parse(last_response.body) match = JSONSelect(".product").match(json) ActiveSupport::JSON.encode(match).should eq(@products.last.to_json) end Then /^the response body should be a JSON representation of the Products list \(Ember Data conventions\)$/ do steps %{ Then the response body should fulfill the Ember Data expectations for Product list } json = JSON.parse(last_response.body) match = JSONSelect(".products").match(json) ActiveSupport::JSON.encode(match).should eq(@products.to_json) end Then /^the JSON response body should list all Products \(Ember Data conventions\)$/ do steps %{ Then the response body should fulfill the Ember Data expectations for Product list } json = JSON.parse(last_response.body) JSONSelect(".products").match(json).length.should eq(@products.count) end Then /^the JSON response body should list (\d+) Products \(Ember Data conventions\)$/ do |n_products| steps %{ Then the response body should fulfill the Ember Data expectations for Product list } json = JSON.parse(last_response.body) JSONSelect(".products").match(json).length.should eq(n_products.to_i) end