Created
May 10, 2014 00:49
-
-
Save t-oginogin/f5f01a95675e8d2a1240 to your computer and use it in GitHub Desktop.
factory_girl_railsで生成したオブジェクトを破壊的メソッドで変更する場合の注意点 ref: http://qiita.com/t_oginogin/items/e5bd1e452d0c4ab714e1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ bundle exec rake test | |
| Run options: --seed 59117 | |
| # Running tests: | |
| [index]report no:R00002 | |
| [index]report no:R00003 | |
| [index]report no:R00004 | |
| [index]report no:R00005 | |
| [index]report no:R00006 | |
| [index]report no:R00007 | |
| [index]report no:R00008 | |
| [index]report no:R00009 | |
| [index]report no:R00010 | |
| [index]report no:R00011 | |
| .[show]report no:R00011 // ここはR00001を期待していた | |
| F | |
| Finished tests in 0.097491s, 20.5147 tests/s, 20.5147 assertions/s. | |
| 1) Failure: | |
| ReportsController::show#test_0001_指定したレポートを取得できる [/Users/xxx/test_app/test/controllers/reports_controller_test.rb:29]: | |
| Expected: "R00001" | |
| Actual: "R00011" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| report_no = FactoryGirl.build(:report_test1).report_no.dup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ be rake test | |
| Run options: --seed 21061 | |
| # Running tests: | |
| [index]report no:R00002 | |
| [index]report no:R00003 | |
| [index]report no:R00004 | |
| [index]report no:R00005 | |
| [index]report no:R00006 | |
| [index]report no:R00007 | |
| [index]report no:R00008 | |
| [index]report no:R00009 | |
| [index]report no:R00010 | |
| [index]report no:R00011 | |
| .[show]report no:R00001 | |
| . | |
| Finished tests in 0.089792s, 22.2737 tests/s, 22.2737 assertions/s. | |
| 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FactoryGirl.define do | |
| factory :report_test1, class: :Report do | |
| report_no 'R00001' | |
| title 'report title' | |
| content 'report content' | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'test_helper' | |
| describe ReportsController do | |
| describe "index" do | |
| before do | |
| report_no = FactoryGirl.build(:report_test1).report_no | |
| 10.times do | |
| report = FactoryGirl.build(:report_test1) | |
| report.report_no = report_no.succ! #破壊的メソッドでインクリメント | |
| puts "[index]report no:#{report.report_no}" | |
| report.save | |
| end | |
| end | |
| it "10件のデータを取得できる" do | |
| get :index | |
| assert_equal 10, assigns(:reports).count | |
| end | |
| end | |
| describe "show" do | |
| before do | |
| report = FactoryGirl.create(:report_test1) | |
| puts "[show]report no:#{report.report_no}" | |
| end | |
| it "指定したレポートを取得できる" do | |
| get :show, id: 1 | |
| assert_equal 'R00001', assigns(:report).report_no | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment