如何使用工厂女孩和黄瓜创建用户记录? [英] How to create a user record using factory girl and cucumber?

查看:231
本文介绍了如何使用工厂女孩和黄瓜创建用户记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有使用工厂女孩,我从这个星期开始测试所以这些新的东西只是让我疯了。任何人都可以解释我一个简单的方法来创建一个使用工厂女孩的记录。

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

我有代码像

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

推荐答案

其他测试不应该依赖于此。对于其他情况,添加给定一个具有其他模型的用户步骤,具有以下定义:

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



在规范/工厂/ users.rb中指定默认属性值,但您可以在 FactoryGirl.create()调用中覆盖它们:

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

在spec / factories / filters.rb中:

In spec/factories/filters.rb:

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

在spec / factories / accounts.rb中(这只是一个空工厂,您可以指定经常需要分配的属性。):

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

而是使用这样的:

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

这篇关于如何使用工厂女孩和黄瓜创建用户记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆