5976

How to create a user record using factory girl and cucumber?

Question:

I have never used factory girl and I started testing from this week so all this new stuff just making me crazy. Can anyone explain me a simple way to create a record using factory girl.

Here is the use case. I already created a test using cucumber where it creates a user and also create other records like account, user categories. Now I am writing second test case where I say there should be no test data(I can use data created from first test but dont want dependency from other test cases.). So I want to use factory girl to create a user for me and associated user records. Here my test which works without using factory girl.

@no-database-cleaner Feature: Managing users parent child relationships In order to use portal I want to create parent child relationships Scenario: Creating a child user Given I am on the homepage When I attempt to sign in with following user account: | email address | password | | abc@company1.com | password | Then I should see "abc@company1.com" message on page When I follow "All Child Users" Then I should see "Add Sub Child" When I click "Add Sub Child" Then I should see "Child Sub User" And I fill in "Email" with "xyztest@gmail.com" And I select "Medium" from "user_filter" And I choose "abc_id_yes" When I press "Create Child User" Then I should see "Child User is successfully created." And appropriate records should get created for the child user for new abc_id

And in step definition I have code like

Then(/^appropriate records should get created for the child user for new abc_id$/) do parent_user = User.find_by_email("abc@company1.com") user = User.find_by_email("xyztest@gmail.com") account = Account.find_by_user_id(parent_user) user.default_filter_level.should be_true user.ar_id.should be_true user.parent_id.should == parent_user.id filter = Filter.find_by_user_id(user.id) filter.user_id.should == user.id filter.abc_id.should be_true filter.account_id.should == account.id end

So how I can achieve same result using factory girl which will also create a associated records. Thanks

Answer1:

You are true that other tests should not depend on this one. For other scenarios, add a Given an user with other models exists step with the following definition:

Given(/^an user with other models exists$/) do parent = FactoryGirl.create(:user) user = FactoryGirl.create(:user, email: "xyztest@gmail.com" parent_id: parent) account = FactoryGirl.create(:account, user_id: user.id) FactoryGirl.create(:filter, user_id: user.id, account_id: account.id) end

In spec/factories/users.rb (you specify the default attribute values, but you can override them in the FactoryGirl.create() call):

FactoryGirl.define do factory :user do email "abc@company1.com" default_filter_level true ar_id true end end

In spec/factories/filters.rb:

FactoryGirl.define do factory :filter do abc_id true end end

In spec/factories/accounts.rb (It's just an empty factory, you can specify the attributes you often need to have assigned.):

FactoryGirl.define do factory :account do end end

Note that in the last step in the scenario, you should not test whether the appropriate models are created:

And appropriate records should get created for the child user for new abc_id

Instead use something like this:

When I follow "All Child Users" Then the page should have newly created child user

Recommend

  • youtube video insert “onBehalfOfContentOwner” parameter value
  • MVC 3 - Editor template for List Model [closed]
  • Is there a way to focus on a dynamically created form on Angular 4/Ionic3?
  • Git Configuration with eclipse--not able to push code from eclipse to remote git server
  • How do I manage org and space users in bluemix using cf command line?
  • C# foreach - Is collection computed with each iteration? [duplicate]
  • Open Graph Publish Using Spring Social
  • Capturing HTML Text Input Key press after key has been applied?
  • Play Framework nested form errors missing
  • Download/Save/Write a file on the client's hard disk using flash/flex
  • CSS: How to fix overlapping divs
  • Spring security - same page to deliver different content based on user role
  • Efficient User-Agent Regex to find Safari in Python
  • Authentication in Play! and RestEasy
  • Force show.bind execution
  • Ajax calls do not work in IE unless you fiddle with security settings
  • How do I access an unhandled exception in an MVC Error view?
  • Email verification using google app script and google forms
  • Allowing both email and username for authentication
  • Typescript - Unable to get 'import' statement to function
  • Meteor: Do Something On Email Verification Confirmation
  • Disable Enter in editText android
  • Cannot resolve symbol 'MyApi'
  • Handling un-mapped Rest path
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Email format validation in mvc3 view
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Fetching methods from BroadcastReceiver to update UI
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Running a C# exe file
  • Cassandra Data Model
  • sending/ receiving email in Java
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • using HTMLImports.whenReady not working in chrome
  • Linking SubReports Without LinkChild/LinkMaster
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Running Map reduces the dimensions of the matrices
  • How to get NHibernate ISession to cache entity not retrieved by primary key