为什么工厂女孩没有对独特的属性进行排序? [英] How come Factory Girl isn't sequencing unique attributes?

查看:11
本文介绍了为什么工厂女孩没有对独特的属性进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器规范失败了,因为 Factory Girl 似乎在创建非唯一用户,即使我对需要唯一的用户属性进行排序.

My controller spec fails because Factory Girl seems to be creating non-unique Users even though I sequence the User attributes that need to be unique.

错误

  1) TopicsController POST #create when topic is invalid should render new
     Failure/Error: let(:invalid_topic) {Factory.build :invalid_topic}
     ActiveRecord::RecordInvalid:Validation failed: Email has already been taken, Username has already been taken

  2) TopicsController POST #create when topic is valid should redirect to show
     Failure/Error: let(:valid_topic) {Factory.build :topic}
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken, Username has already been taken

控制器规范 (RSpec)

  describe "POST #create" do                          
    let(:valid_topic) {Factory.build :topic}
    let(:invalid_topic) {Factory.build :invalid_topic}

    context "when topic is invalid" do
      it "should render new" do
        post :create, :topic => invalid_topic
        response.should render_template(:new)
      end
    end
    context "when topic is valid" do
      it "should redirect to show" do
        post :create, :topic => valid_topic
        response.should redirect_to(topic_path(assigns(:topic)))
      end
    end
  end

工厂

Factory.define :user do |f|
  f.sequence(:username) { |n| "foo#{n}"}
  f.password "password"
  f.password_confirmation { |u| u.password}
  f.sequence(:email) { |n| "foo#{n}@example.com"}
end

Factory.define :topic do |f|
  f.name "test topic"
  f.association :creator, :factory => :user
  f.forum_id 1
end

当我使用 Factory.create :topic 时,为什么 Factory Girl 没有对用户属性进行排序?

Why isn't Factory Girl sequencing the User attributes when I use Factory.create :topic?

推荐答案

rake db:test:prepare 似乎解决了这个问题.

rake db:test:prepare seemed to fix the problem.

不知道为什么.架构没有改变.

Not sure why, though. The schema hadn't been changed.

这篇关于为什么工厂女孩没有对独特的属性进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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