RoR Minitest赛程:“找不到帖子'id'= 5025348”/如何“测试”赛程?

时间:2016-02-20 11:20:58

标签: ruby-on-rails minitest fixtures

“突然”我收到此错误,我无法弄清楚原因。

  • 也许你看到了问题或......
  • 有没有办法“测试”灯具?
  

错误[“test_should_redirect_create_when_not_logged_in”(...)       ActiveRecord :: RecordNotFound:无法找到帖子'id'= 415950658                   test / controllers / text_posts_controller_test.rb:6:在`setup'

测试/控制器/ 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 

赛程:posts.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 %>

Fixtures:users.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 %>

schema.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

1 个答案:

答案 0 :(得分:0)

现在有效。我做了重置和新种子。

  

rake db:migrate:reset

     

rake db:seed