- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个 POST 模型、 Controller 和 View ,我目前正在创建它的测试套件以确保所有内容都经过正确测试。我现在正在创建集成测试,并且似乎总是遇到相同的错误,即在运行“成功编辑帖子”测试时,ID 丢失。
我真的不明白我做错了什么。错误消息说缺少一个 :id 键,但是在创建新帖子时不应该自动创建一个吗?我做错了什么?
版本:
ruby :2.1.2
rails :4.2.1
集成测试
require 'test_helper'
class PostsCrudTest < ActionDispatch::IntegrationTest
def setup
@post = { title: "This is the title",
content: "Detailed comment."*10,
phone: 9991118888,
email: "email@h_list.com",
user_id: users(:spiderman).id }
end
test "creates a new post successfully" do
get new_post_path
assert_template 'posts/new'
assert_difference 'Post.count', 1 do
post posts_path, post: @post
end
assert_template 'posts/show'
end
test "fails to create a new post with inaccurate info" do
get new_post_path
assert_no_difference 'Post.count' do
post posts_path, post: { title: "", content: "", phone: 1111, email: 00 }
end
assert_template 'posts/new'
end
test "shows a new post correctly" do
get new_post_path
post posts_path, post: @post
assert_template 'posts/show'
assert_equal 'Your new posting was created.', flash[:success]
assert_select 'h2', @post[:title]
assert_select 'h2+p', @post[:content]
end
test "edits a post successfully" do
get new_post_path
post posts_url, post: @post
assert_template 'posts/show'
get edit_post_path, post: @post
# assert_select '.edit_post' do
assert_select 'textarea', @post[:content]
# end
end
end
测试结果
$ rake test:integration
Run options: --seed 35902
# Running:
...E
Finished in 0.310827s, 12.8689 runs/s, 32.1722 assertions/s.
1) Error:
PostsCrudTest#test_edits_a_post_successfully:
ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"posts"} missing required keys: [:id]
test/integration/posts_crud_test.rb:44:in `block in <class:PostsCrudTest>'
4 runs, 10 assertions, 0 failures, 1 errors, 0 skips
相关路线:
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
注意:一切似乎在实际站点上运行良好。只是测试套件给我这个错误。
最佳答案
The error message says that an :id key is missing
是的,因为您正在执行两个步骤:
您使用@post 散列创建了一个新帖子。我假设帖子是在服务器上创建的,这通常意味着帖子是在数据库中创建的,并且会创建一个新的 ID。
您尝试编辑没有任何 ID 的 @post 散列。这就是您收到错误消息的原因。
有多种选择可以解决这个问题。
选项 1 很简单:直接在代码中创建帖子,而不是使用 new_post_path。
例子:
test "edits a post successfully" do
p = Post.create(@post) # or whatever options you want
get edit_post_path, post: p.id
...
选项 2 是高级的:像您一样创建帖子,并让您的服务器返回新创建帖子的 ID。如果需要,您可以将它放在 HTTP 响应中,或者将它放在更具结构性的内容中,例如 JSON 响应。
关于ruby-on-rails - MiniTest - 找不到路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30381356/
我正在阅读 minitest 的模拟功能。 require "minitest/autorun" mock = MiniTest::Mock.new mock.expect(:use_any_stri
当我运行我的规范时,我得到这个错误,即使我从来没有在我的代码中的任何地方包含 minitest: Warning: you should require 'minitest/autorun' inst
我一直在使用 MiniTest 学习 TDD/BDD。我想弄清楚的是我的代码的哪些部分应该使用 MiniTest::Unit::TestCase 进行测试,哪些部分应该使用 MiniTest::Spe
请帮忙: 我想用 minitest 来使用 shoulda。 这是我得到的异常: NoMethodError: undefined method `run_teardown_hooks' for #:
请帮忙: 我想用 minitest 来使用 shoulda。 这是我得到的异常: NoMethodError: undefined method `run_teardown_hooks' for #:
我正在尝试启动并运行我的模型测试,但我的终端出现错误 rake aborted! uninitialized class variable @@current in MiniTest::Unit::T
考虑将 Minitest 用于现有的 Rails 3.2。 我试图了解 minitest-rails 和 minitest-spec-rails 之间的区别。 最佳答案 minitest-spec-r
我一直在使用 Ruby 2.1 附带的 MiniTest,没有任何问题。我将子类化 MiniTest::Unit:TestCase 创建几个方法,如“test_simple”,一切正常。我会毫无问题地
我正在使用 minitest 的规范语法和描述 block 。在我的 minitest_helper.rb (每个规范文件需要)中,我有: Minitest::Test.i_suck_and_my_t
我正在使用 minitest 的规范语法和描述 block 。在我的 minitest_helper.rb (每个规范文件需要)中,我有: Minitest::Test.i_suck_and_my_t
在 Ruby 1.9.1 中,我发现 ctrl + c 只会杀死一个单元测试,而你无法停止整个测试程序的运行方式。 相比之下,在 Ruby 1.8 中的 test/unit 下,它会停止所有测试。 1
我有一个方法“退出”: def quit puts "Good bye!" exit end 我想做的是做一个小型测试断言 quit 方法确实退出了,但我尝试过的都不起作用。寻找输入。提前致谢
我正在尝试在我的代码中测试该方法,但第二个测试返回错误 undefined local variable or method 'params' 测试方法的正确方法是什么?还是我需要对 main.rb
Ruby 1.9 有很棒的 Unicode 支持,是吗? # encoding: utf-8 require 'minitest/spec' require 'minitest/autorun' de
我使用 minitest 为我的网络项目编写测试。 我有一个模拟对象,模拟了 3 个方法。该对象表示具有多个属性的数据库实体(hanami 模型)。现在,如果我多次为一个属性调用“getter”,我会
我在我的迷你测试中使用自定义断言,我想对我的断言进行单元测试。当然,我可以测试快乐路径,但我想断言测试实际上失败了。 module Minitest module Assertions d
我正在查看我的测试用例结果,很难看出我的测试中的一个小故障是从哪里来的。 我正在处理合理大小的数据结构 - 我不想更改 to_s 方法,以便它对 minitest diff 稍微好一些。 我看过记者,
是否有用于 minitest 的替代测试报告器,它会在测试运行时显示哪些测试已损坏以及错误消息是什么,而不必等到结束。 最佳答案 你可以看看 minitest-reporters gem 。它允许您以
(已在 https://www.ruby-forum.com/topic/6876320 发布,但在这里交叉发布,因为到目前为止我还没有收到回复)。 关于在 Minitest 和/或 Test::Un
每当我尝试 assert_equal 两个对象时,我总是会遇到这样的错误: No visible difference in the User#inspect output. You sh
我是一名优秀的程序员,十分优秀!