为什么工厂女孩不排序独特的属性?

发布于 2024-11-09 05:28:20 字数 1714 浏览 2 评论 0原文

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

错误

  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 Girl 不排序用户属性Factory.create :主题?

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.

The Errors

  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

The Controller Spec (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

The Factories

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄暮涼年 2024-11-16 05:28:20

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

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

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

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

是你 2024-11-16 05:28:20

请考虑使用 database_cleaner gem。其中一种是专门为了实现在测试运行之间清理数据库的目的而设计的。

这篇文章几乎解释了一切。

Please, consider using database_cleaner gem. One was designed specifically to fulfill the purpose of cleaning up database between test runs.

This post explains pretty much everything.

笑叹一世浮沉 2024-11-16 05:28:20

您应该考虑在测试结束时手动删除所有主题。当然,这不是第一解决方案,但对我来说效果非常好。

after(:all) { Topic.delete_all }

You should consider deleting all topics by hand in the end of the test. Of course, it is not number one solution but worked out for me real great.

after(:all) { Topic.delete_all }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文