首页 \ 问答 \ 工厂女孩 - 有很多协会(Factory Girl - has many associations)

工厂女孩 - 有很多协会(Factory Girl - has many associations)

我使用rspec的工厂女孩​​,这里是我的:

factories.rb

Factory.define :user do |f|
  f.sequence(:fname) { |n| "fname#{n}" }
  f.sequence(:lname) { |n| "lname#{n}" }
  f.sequence(:email) { |n| "email#{n}@google.com" }
  f.password               "password"
  f.password_confirmation { |u| u.password }
  f.invitation_code        "xxxxxxx"
end

Factory.define :group do |f|
  f.sequence(:name) { |n| "myGroup#{n}" }
  f.sequence(:private_email) { |n| "myGroup#{n}" }
end

Factory.define :permission do |f|
  f.role_id         1
end

groups_controller_spec.rb

describe GroupsController do
  include Devise::TestHelpers

  before (:each) do
    @user1 = Factory.create(:user)
    @user1.confirm!
    sign_in @user1
    @group = Factory.create(:group)
    @group2 = Factory.create(:group)
    @group3 = Factory.create(:group)
    @permission_user_1 = Factory.create(:permission, :user => @user1, :creator_id => @user1.id, :group => @group)
    @permission_user_2 = Factory.create(:permission, :user => @user1, :creator_id => @user1.id, :group => @group2)
    @permission_user_3 = Factory.create(:permission, :user => @user1, :creator_id => @user1.id, :group => @group3)
  end

.......

问题是组模型有一个user_id字段,该字段未由工厂设置。 我如何创建一个用户,然后让工厂代表该用户创建一个组。 像@ user1.group.factory.create(:group)?

有任何想法吗? 谢谢


I'm using factory girl with rspec, here is what I have:

factories.rb

Factory.define :user do |f|
  f.sequence(:fname) { |n| "fname#{n}" }
  f.sequence(:lname) { |n| "lname#{n}" }
  f.sequence(:email) { |n| "email#{n}@google.com" }
  f.password               "password"
  f.password_confirmation { |u| u.password }
  f.invitation_code        "xxxxxxx"
end

Factory.define :group do |f|
  f.sequence(:name) { |n| "myGroup#{n}" }
  f.sequence(:private_email) { |n| "myGroup#{n}" }
end

Factory.define :permission do |f|
  f.role_id         1
end

groups_controller_spec.rb

describe GroupsController do
  include Devise::TestHelpers

  before (:each) do
    @user1 = Factory.create(:user)
    @user1.confirm!
    sign_in @user1
    @group = Factory.create(:group)
    @group2 = Factory.create(:group)
    @group3 = Factory.create(:group)
    @permission_user_1 = Factory.create(:permission, :user => @user1, :creator_id => @user1.id, :group => @group)
    @permission_user_2 = Factory.create(:permission, :user => @user1, :creator_id => @user1.id, :group => @group2)
    @permission_user_3 = Factory.create(:permission, :user => @user1, :creator_id => @user1.id, :group => @group3)
  end

.......

Problem is the group model has a field for user_id which is not being set by factory. How can I create a users, and then have factory create a group on behalf of that user. Like @user1.group.factory.create(:group)?

Any ideas? Thanks


原文:https://stackoverflow.com/questions/6003871
更新时间:2024-03-04 20:03

最满意答案

绿洲是不可能的,你需要修改你的_tags文件,但我不建议你这样做。 一般方法是在您的绿洲文件中创建Library条目。 这有助于保持项目结构清洁。 如果你不想从utils创建一个库,那么为什么要烦恼并将它移动到一个子文件夹中。


It is impossible with oasis, you need to modify your _tags file, but I wouldn't advice you to do this. The general approach is to create Library entries in your oasis file. This helps to keep your project structure clean. And if you wouldn't like to create a library from utils, then why bother and move it into a subfolder.

相关问答

更多
  • 我认为最好的解决方案是摆脱递归make并且只有顶层的Makefile.am 。 然后你只需添加一个规则 include/autogen-file.h src/autogen-file.cxx: myscript.sh ${SHELL} $< 并在BUILT_SOURCES列出include/autogen-file.h BUILT_SOURCES 。 如果要保留递归结构(即使这被认为是有害的 ),您可以将规则生成到子目录中的Makefile.am中。 在src/Makefile.am : auto ...
  • 您可以将除了http(index.php,css,images等)所需的文件之外的所有文件从DocumentRoot目录移动到更高级别,因此目录布局如下所示: /lib /files /html /index.php /css/ /images/ 其中/ html是您的DocumentRoot。 在这种情况下,您不需要.htaccess或VirtualHost配置中的任何其他限制性规则/ You can move all files except ones needed to be ac ...
  • 我认为问题在于您的location /块正在捕获您的动态请求以及您的非资产静态请求。 试试这个配置(注意try_files行): server { listen 80 default deferred; # server_name example.com; root /home/user/appname/public; try_files $uri @app; location ^~ /assets/ { root /home/user/appname/public; ...
  • 绿洲是不可能的,你需要修改你的_tags文件,但我不建议你这样做。 一般方法是在您的绿洲文件中创建Library条目。 这有助于保持项目结构清洁。 如果你不想从utils创建一个库,那么为什么要烦恼并将它移动到一个子文件夹中。 It is impossible with oasis, you need to modify your _tags file, but I wouldn't advice you to do this. The general approach is to create Libra ...
  • 尝试这个.. func main() { fmt.Println("Removing build directory") c := exec.Command("cmd", "/C", "rd /S /Q", "C:\\build") if err := c.Run(); err != nil { fmt.Println("Error: ", err) } if err := exec.Command("cmd", "/C", "mkdir" ...
  • 使用excludes="META-INF/**""对我有用(Ant 1.9.3,Windows 7,JDK 1.7.0_51) 从这里下载原始commons-logging-1.1.3.jar后进行一些测试:
  • 最简单的方法是将Makefile移动到root/并在其顶部添加这些行: APPS := app1 app2 ... TARGETS := $(addsuffix /build, $(APPS)) all: $(TARGETS) %/build: $(MAKE) -C $* -f ../Makefile build 如果您不想以递归方式使用Make,那么我们必须查看makefile的内部,以便了解如何修改它。 The simplest way is to move the Makefile up ...
  • 该库必须存在于GAC或引用程序集所在的同一目录中。您可以手动复制它以检查是否可以解决问题 - 如果是,则添加任务以确保引用是出现在输出问题中。 The library has to be present either in GAC or in the same directory that the referencing assembly is in. You can copy it manually to check if this fixes the problem - if yes, th ...
  • 问题现在解决了,我正在回答我自己的问题。 首先,我必须再次使用--force选项添加'app / build'目录,然后删除整个'app / build'目录。 那就是诀窍。 我想前面的提交会被中断,并将目录置于'未定义'状态。 Problem is solved now, I am answering my own question. First I had to add the 'app/build' directory again with the --force option and then I ...
  • 开始状态: testsub\xxx_xxx.dita 期望的结果: testsub\xxx_xxx\xxx_xxx.dita 对于单个正则表达式映射器,这应该是可行的,您根本不需要foreach :

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。