当我尝试测试Active Model Serializers时,我在 ActionController上尝试执行修补程序:更新或发布:更新时遇到了问题: :测试用例即可。这是中止输出:
(9678, 38742, 30, '2015-06-11 20:36:31', 'Kicked: Don\'t abuse /report'),
以下是流派控制器测试,只有相关的测试用例。可以通过此link找到设置,拆解和所有其他测试。
deovandski@deovandski-VirtualBox:~/Fakktion$ rake test
rake aborted!
SyntaxError: /home/deovandski/Fakktion/test/controllers/api/v1/genres_controller_test.rb:46: syntax error, unexpected '\n', expecting =>
/home/deovandski/Fakktion/test/controllers/api/v1/genres_controller_test.rb:54: syntax error, unexpected keyword_do_block, expecting keyword_end
/home/deovandski/Fakktion/test/controllers/api/v1/genres_controller_test.rb:58: syntax error, unexpected keyword_end, expecting end-of-input
/home/deovandski/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:244:in `rescue in load_dependency'
/home/deovandski/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:237:in `load_dependency'
/home/deovandski/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/home/deovandski/.rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/home/deovandski/.rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/home/deovandski/.rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/home/deovandski/.rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/home/deovandski/.rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/home/deovandski/.rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/home/deovandski/.rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run
(See full trace by running task with --trace)
根似乎是require 'test_helper'
class Api::V1::GenresControllerTest < ActionController::TestCase
test "API - Update a genre" do
genre = Genre.find_by name: 'test'
genre.name = "mikuchan"
patch :update, id: genre.id, ActiveModel::SerializableResource.new(genre).as_json
genreUpdated = Genre.find_by name: 'mikuchan'
assert_response :success, genreUpdated
end
end
从上面开始的行,因为当期望 =&gt; 时,abort表示意外的 / n 。但是,当我删除patch :update
时,会出现以下错误:
id: genre.id
上面的输出看起来像一个正确的有效负载,所以问题似乎与语法有关。但是,我发现的唯一另一种方法是使用 genre.save ,这不是我想要的,因为此过程仅使用ActiveRecord ......
有了这个,测试帖子的正确方法是什么:在AMS上更新?此外,如果我使用:json_api 作为适配器,程序会改变吗?
答案 0 :(得分:0)
这是一个语法问题,因为适当的语法是:
tempResource = ActiveModel::SerializableResource.new(resource).serializable_hash
post :update, tempResource.merge(id: resource)
这是测试资源的CREATE,SHOW,PATCH和DELETE的示例:
require 'test_helper'
class Api::V1::GenresControllerTest < ActionController::TestCase
# Called before every test
setup do
@request.headers["Content-Type"] = "application/json; charset=utf-8"
@testGenre = Genre.new(name: 'TEST', eligibility_counter: 0, posts_count: 0)
@testGenre.save
end
# Called after test
def teardown
@testGenre = nil
@apiGenre = nil
@genreSerialized = nil
end
test "Genres - API - Get Index" do
get :index
assert_response_schema('genres/index.json')
end
test "Genres - API - Serializer Validation" do
sampleSenre = Genre.new
serializer = ActiveModel::Serializer.serializer_for(sampleSenre)
assert_equal GenreSerializer, serializer
end
test "Genres - API - Create 200" do
apiGenre = Genre.new(name: 'luka', eligibility_counter: 0, posts_count: 0)
assert_difference('Genre.count', +1) do
post :create, ActiveModel::SerializableResource.new(apiGenre).as_json
end
end
test "Genres - API - Create 422" do
post :create, ActiveModel::SerializableResource.new(@testGenre).as_json
assert_response(422)
end
test "Genres - API - SHOW 200" do
get :show, id: @testGenre
assert_response_schema('genres/show.json')
end
test "Genres - API - UPDATE 200" do
genre = Genre.find_by name: 'test'
genre.name = "mikuchan"
tempGenre = ActiveModel::SerializableResource.new(genre).serializable_hash
post :update, tempGenre.merge(id: genre)
genreUpdated = Genre.find_by name: 'mikuchan'
assert_response :success, genreUpdated
end
test "Genres - API - UPDATE 422" do
genre = Genre.find_by name: 'test'
genre1 = Genre.find_by name: 'movie'
genre.name = "mikuchan"
genre1.name = "mikuchan"
tempGenre = ActiveModel::SerializableResource.new(genre).serializable_hash
tempGenre1 = ActiveModel::SerializableResource.new(genre1).serializable_hash
post :update, tempGenre.merge(id: genre)
post :update, tempGenre1.merge(id: genre1)
assert_response(422)
end
test "Genres - API - DELETE 200" do
assert_difference('Genre.count', -1) do
delete :destroy, id: @testGenre
end
end
test "Genres - API - DELETE 422" do
genre = Genre.find_by name: 'song'
post = Post.first
post.genre = genre
post.save
delete :destroy, id: genre
assert_response(422)
end
end