在rails app上运行ruby时出错

时间:2018-01-29 11:53:29

标签: ruby-on-rails ruby rvm

我在mac上的rails上安装了rvm和ruby。安装完所有的bundle之后,当我运行rails -s命令时,我遇到了这些错误。由于我是ruby on rails的新手,我不知道这些错误是什么意思,我怎么解决它们。

还添加了posts_controller_test.erb的代码

Running:
E
Error:
PostsControllerTest#test_should_get_new:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: authors.email: INSERT INTO "authors" ("created_at", "updated_at", "id") VALUES ('2018-01-29 11:46:00.853708', '2018-01-29 11:46:00.853708', 298486374)
bin/rails test test/controllers/posts_controller_test.rb:13
E
Error:
PostsControllerTest#test_should_show_post:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: authors.email: INSERT INTO "authors" ("created_at", "updated_at", "id") VALUES ('2018-01-29 11:46:01.050540', '2018-01-29 11:46:01.050540', 298486374)
bin/rails test test/controllers/posts_controller_test.rb:26
E
Error:
PostsControllerTest#test_should_get_edit:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: authors.email: INSERT INTO "authors" ("created_at", "updated_at", "id") VALUES ('2018-01-29 11:46:01.056155', '2018-01-29 11:46:01.056155', 298486374)
bin/rails test test/controllers/posts_controller_test.rb:31
E
Error:
PostsControllerTest#test_should_create_post:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: authors.email: INSERT INTO "authors" ("created_at", "updated_at", "id") VALUES ('2018-01-29 11:46:01.062023', '2018-01-29 11:46:01.062023', 298486374)
bin/rails test test/controllers/posts_controller_test.rb:18
E
Error:
PostsControllerTest#test_should_get_index:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: authors.email: INSERT INTO "authors" ("created_at", "updated_at", "id") VALUES ('2018-01-29 11:46:01.068163', '2018-01-29 11:46:01.068163', 298486374)
bin/rails test test/controllers/posts_controller_test.rb:8
E
Error:
PostsControllerTest#test_should_destroy_post:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: authors.email: INSERT INTO "authors" ("created_at", "updated_at", "id") VALUES ('2018-01-29 11:46:01.073382', '2018-01-29 11:46:01.073382', 298486374)
bin/rails test test/controllers/posts_controller_test.rb:41
E
Error:
PostsControllerTest#test_should_update_post:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: authors.email: INSERT INTO "authors" ("created_at", "updated_at", "id") VALUES ('2018-01-29 11:46:01.079061', '2018-01-29 11:46:01.079061', 298486374)   
bin/rails test test/controllers/posts_controller_test.rb:36
Finished in 0.258853s, 27.0424 runs/s, 0.0000 assertions/s.
7 runs, 0 assertions, 0 failures, 7 errors, 0 skips

posts_controller_test.erb

require 'test_helper'

class PostsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @post = posts(:one)
  end

test "should get index" do
 get posts_url
  assert_response :success
end

test "should get new" do
 get new_post_url
  assert_response :success
 end

test "should create post" do
  assert_difference('Post.count') do
  post posts_url, params: { post: { body: @post.body, description: @post.description, slug: @post.slug, title: @post.title } }
  end

  assert_redirected_to post_url(Post.last)
  end

  test "should show post" do
   get post_url(@post)
   assert_response :success
  end

  test "should get edit" do
  get edit_post_url(@post)
  assert_response :success
  end

   test "should update post" do
   patch post_url(@post), params: { post: { body: @post.body, 
   description: @post.description, slug: @post.slug, title: @post.title 
    } }
   assert_redirected_to post_url(@post)
   end

  test "should destroy post" do
   assert_difference('Post.count', -1) do
  delete post_url(@post)
  end

   assert_redirected_to posts_url
 end
 end

1 个答案:

答案 0 :(得分:0)

“唯一约束”表示您尝试多次在author email字段上插入相同的值,而您的数据库设置为仅允许唯一值。

没有剩下的代码就无法判断,但看起来你的测试要么没有正确重置,要么你试图多次创建同一个作者。

尝试在posts_controller_test.rb文件中发布代码。