如何使用继承用户模型定义工厂 [英] how to define factories with a inheritance user model

查看:65
本文介绍了如何使用继承用户模型定义工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题: 在我的应用程序中,我使用继承来定义用户模型:

I got following problem: In my application i use inheritance to define my user model:

class User
 include Mongoid::Document

 field :name...
 field :bla...
end


class CustomUser < User
 field :customuserfield...
end

我如何编写工厂以在我的规格中映射此Class hirachie. 并且继续写作,不要重复自己.

How can i write factories to map this Class hirachie in my specs. And keep up writing with don´t repeat yourself.

FactoryGirl.define do 
  factory :user do
    name  "name"
    bla "bla"

    factory :custom_user do
      customfield "customfield"
    end
  end
end

这对我不起作用,因为该班级也是"User". 使用用户",我得到了一个无效错误,因为在这里没有自定义字段. 有没有好的方法,方法或方法来关联类似的东西.

This doesn´t work for me because the class is also "User". With "User" i got a invalid error because the customfields are not defiend here. Is there a good practice, way or method to relize something like that.

推荐答案

您可以尝试以下操作:

factory :user do
  name  "name"
  bla "bla"
end

factory :custom_user, class: CustomUser, parent: :user do
  customfield "customfield"
end

有关更多信息:继承.

这篇关于如何使用继承用户模型定义工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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