{"id":2689,"date":"2022-08-30T15:26:57","date_gmt":"2022-08-30T15:26:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/06\/switching-from-fixtures-to-factory-girl-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:57","modified_gmt":"2022-08-30T15:26:57","slug":"switching-from-fixtures-to-factory-girl-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/switching-from-fixtures-to-factory-girl-collection-of-common-programming-errors\/","title":{"rendered":"switching from fixtures to Factory Girl-Collection of common programming errors"},"content":{"rendered":"<p>Rails 3.0.3&#8230;.<\/p>\n<p>I&#8217;m just starting out with Factory Girl, having had little success with the standard fixtures approach. I&#8217;ve commented out <code>fixtures :all<\/code> from the test\/test_helper.rb file and have created a factories file.<\/p>\n<p>My problem is that the sequence feature doesn&#8217;t seem to work:<\/p>\n<pre><code># test\/factories.rb\nFactory.sequence :clearer_name do |n|\n   \"Clearer_#{n}\"\nend\n\nFactory.define :clearer do |f|\n   f.name Factory.next(:clearer_name)\nend\n<\/code><\/pre>\n<p>My (functional) test is only slightly different from standard:<\/p>\n<pre><code>require 'test_helper'\n\nclass ClearersControllerTest &lt; ActionController::TestCase\n   setup do\n      @clearer = Factory.create(:clearer)\n   end\n\ntest \"should get index\" do\n   get :index\n   assert_response :success\n   assert_not_nil assigns(:clearers)\n end\n\n test \"should get new\" do\n   get :new\n   assert_response :success\n end\n\n test \"should create clearer\" do\n   assert_difference('Clearer.count') do\n     post :create, :clearer =&gt; @clearer.attributes\n   end\n\n   assert_redirected_to clearer_path(assigns(:clearer))\n end\n<\/code><\/pre>\n<p>When I run <code>rake test<\/code> I get:<\/p>\n<pre><code>test_should_create_clearer(ClearersControllerTest):\nActiveRecord::RecordNotUnique: SQLite3::ConstraintException: column name is not unique: INSERT INTO \"clearers\" (\"active\", \"updated_at\", \"name\", \"created_at\") VALUES ('t', '2011-02-20 08:53:37.040200', 'Clearer_1', '2011-02-20 08:53:37.040200')\n<\/code><\/pre>\n<p>&#8230;as if it&#8217;s not continuing the sequence.<\/p>\n<p>Any tips?<\/p>\n<p>Thanks,<\/p>\n<p><strong>UPDATE<\/strong>: heres my test file:<\/p>\n<pre><code>#clearers_controller_test.rb\nrequire 'test_helper'\nclass ClearersControllerTest &lt; ActionController::TestCase\n  setup do\n    @clearer = Factory.create(:clearer)\n  end\n\n  test \"should create clearer\" do\n\n    assert_difference('Clearer.count') do\n      # does not work without this:\n      Clearer.destroy_all \n      post :create, :clearer =&gt; @clearer.attributes\n    end\nend\n<\/code><\/pre>\n<p>I can get this to work by putting <code>Clearer.destroy_all<\/code> at the top of the test method as shown, but that doesn&#8217;t feel right.<\/p>\n<ol>\n<li>\n<p>I see &#8211; In your setup, you&#8217;re creating a Clearer instance. The Factory.create method builds and saves a new record and returns it.<\/p>\n<p>The problem is that you&#8217;re then attempting to create another instance in your &#8220;should create clearer&#8221; test but you&#8217;re re-using the existing instance&#8217;s attributes.<\/p>\n<p>If you want Factory to return fresh attributes (and the next name sequence), you need to ask it for new attributes:<\/p>\n<pre><code>test \"should create clearer\" do\n  assert_difference('Clearer.count') do\n    post :create, :clearer =&gt; Factory.attributes_for(:clearer)\n  end\nend\n<\/code><\/pre>\n<p>You should only be using that existing @clearer instance in the context of an existing record, not where you want a new one.<\/p>\n<\/li>\n<li>\n<p>You aren&#8217;t starting with a fresh database is my guess. There&#8217;s lots of reasons this could be happen, but you can verify that&#8217;s the problem by adding a Clearer.destroy_all in your setup function, before creating one.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-06 03:58:22. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Rails 3.0.3&#8230;. I&#8217;m just starting out with Factory Girl, having had little success with the standard fixtures approach. I&#8217;ve commented out fixtures :all from the test\/test_helper.rb file and have created a factories file. My problem is that the sequence feature doesn&#8217;t seem to work: # test\/factories.rb Factory.sequence :clearer_name do |n| &#8220;Clearer_#{n}&#8221; end Factory.define :clearer do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2689","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2689"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2689\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}