RoR 迷你测试夹具:"Couldn't find Post with 'id'=5025348" / 如何"test"夹具?



"突然" 我收到此错误,我不知道为什么。

  • 也许您看到了问题或...
  • 有没有办法"测试"夹具?

错误["test_should_redirect_create_when_not_logged_in" (...) ActiveRecord::RecordNotFound:找不到带有"id"=415950658的帖子 test/controllers/text_posts_controller_test.rb:6:in 'setup'

test/controllers/text_posts_controller_test.rb

require 'test_helper'
class TextPostsControllerTest < ActionController::TestCase
  # HERE IS THE PROBLEM @post
  def setup
    @post = posts(:text_post_archer)
  end
  # This test works fine
  test "should redirect create when not logged in" do
    assert_no_difference 'Post.count' do
      post :create, text_post: { title: "Lorem ipsum",
                                 body: "body",
                                 type: "TextPost" }
    end
    assert_redirected_to login_url
  end
  test "should redirect destroy when not logged in" do
    assert_no_difference 'Post.count' do
      delete :destroy, id: @post
    end
    assert_redirected_to login_url
  end
(...)
end 

赛程: 帖子.yml

text_post:
  title: "Title One"
  body: "Body One"
  type: TextPost
  user: michael
  created_at: <%= 10.minutes.ago %>
text_post_archer:
  title: "Title Two"
  body: "Body Two"
  type: TextPost
  user: archer
  created_at: <%= 10.minutes.ago %>
image_post:
  title: "Title Two"
  url: "http://i.imgur.com/Y7syDEa.jpg"
  type: ImagePost
  user: archer
  created_at: <%= 3.years.ago %>

赛程: 用户.yml

michael:
  name: Michael
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: <%= Time.zone.now %>
archer:
  name: Sterling
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

模式.rb

ActiveRecord::Schema.define(version: 20160217191730) do
  (...)
  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.text     "body"
    t.string   "url"
    t.string   "type"
    t.integer  "user_id"
    t.datetime "created_at",                 null: false
    t.datetime "updated_at",                 null: false
    t.integer  "comments_count", default: 0, null: false
    t.string   "picture"
  end
  add_index "posts", ["user_id", "created_at"], name: "index_posts_on_user_id_and_created_at"
  add_index "posts", ["user_id"], name: "index_posts_on_user_id"
   (...)
  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at",                        null: false
    t.datetime "updated_at",                        null: false
    t.string   "password_digest"
    t.string   "remember_digest"
    t.boolean  "admin",             default: false
    t.string   "activation_digest"
    t.boolean  "activated",         default: false
    t.datetime "activated_at"
    t.string   "reset_digest"
    t.datetime "reset_sent_at"
  end
  add_index "users", ["email"], name: "index_users_on_email", unique: true
end

text_post.rb:TextPost模型继承自Post!

 class TextPost < Post
      validates :body, presence: true, 
                    length: { maximum: 12700 } 
end

它现在可以工作了。我做了一个重置和一个新的种子。

耙子 db:迁移:重置

耙子数据库:种子