Rails JSON_API 创建带有流派列表的图书
Rails JSON_API create Book with list of Genres
我正在尝试编写我的测试以确保创建一个新的 book
并分配给它的 genres
有效。
我正在使用具有 JSON_API 结构的活动模型序列化器 (http://jsonapi.org/)
图书模型文件
class Book < ApplicationRecord
belongs_to :author, class_name: "User"
has_and_belongs_to_many :genres
end
流派模型文件
class Genre < ApplicationRecord
has_and_belongs_to_many :books
end
图书序列化程序文件
class BookSerializer < ActiveModel::Serializer
attributes :id, :title, :adult_content, :published
belongs_to :author
has_many :genres
end
测试样本数据
def setup
...
@fantasy = genres(:fantasy)
@newbook = {
title: "Three Little Pigs",
adult_content: false,
author_id: @jim.id,
published: false,
genres: [{title: 'Fantasy'}]
}
end
测试方法
test "book create - should create a new book" do
post books_path, params: @newbook, headers: user_authenticated_header(@jim)
assert_response :created
json = JSON.parse(response.body)
puts "json = #{json}"
assert_equal "Three Little Pigs", json['data']['attributes']['title']
genre_data = json['data']['relationships']['genres']['data']
puts "genre_data = #{genre_data.count}"
assert_equal "Fantasy", genre_data
end
预订强参数
def book_params
params.permit(:title, :adult_content, :published, :author_id, :genres)
end
测试结果(控制台响应)
# Running:
......................................................json = {"data"=>{"id"=>"1018350796", "type"=>"books", "attributes"=>{"title"=>"Three Little Pigs", "adult-content"=>false, "published"=>false}, "relationships"=>{"author"=>{"data"=>{"id"=>"1027431151", "type"=>"users"}}, "genres"=>{"data"=>[]}}}}
genre_data = 0
F
Failure:
BooksControllerTest#test_book_create_-_should_create_a_new_book [/Users/warlock/App_Projects/Raven Quill/Source Code/Rails/raven-quill-api/test/controllers/books_controller_test.rb:60]:
Expected: "Fantasy"
Actual: []
bin/rails test test/controllers/books_controller_test.rb:51
Finished in 1.071044s, 51.3518 runs/s, 65.3568 assertions/s.
55 runs, 70 assertions, 1 failures, 0 errors, 0 skips
正如您从我的 JSON 控制台日志中看到的那样,我的流派似乎没有被设置(需要在上面的测试输出中滚动到右侧)。
请忽略这一行:
assert_equal "Fantasy", genre_data
我知道那是错误的。目前,json 显示 genre => {data: []}
(空数组),这就是我目前要解决的问题。
在这种情况下,我该如何着手创作一本有流派的书,有什么想法吗? :D
这真是令人难过...这周第三次,我正在回答我自己的问题。
我终于从这个 Whosebug 问题中找到了答案:
HABTM association with Strong Parameters is not saving user in Rails 4
原来我的强参数需要是:
def book_params
params.permit(:title, :adult_content, :published, :author_id, {:genre_ids => []})
end
那么我的测试数据可以是:
@fantasy = genres(:fantasy)
@newbook = {
title: "Three Little Pigs",
adult_content: false,
author_id: @jim.id,
published: false,
genre_ids: [@fantasy.id]
}
将我的测试方法更新为:
test "book create - should create a new book" do
post books_path, params: @newbook, headers: user_authenticated_header(@jim)
assert_response :created
json = JSON.parse(response.body)
assert_equal "Three Little Pigs", json['data']['attributes']['title']
genre = json['data']['relationships']['genres']['data'].first['title']
assert_equal "Fantasy", genre
end
现在我的测试通过了。
我正在尝试编写我的测试以确保创建一个新的 book
并分配给它的 genres
有效。
我正在使用具有 JSON_API 结构的活动模型序列化器 (http://jsonapi.org/)
图书模型文件
class Book < ApplicationRecord
belongs_to :author, class_name: "User"
has_and_belongs_to_many :genres
end
流派模型文件
class Genre < ApplicationRecord
has_and_belongs_to_many :books
end
图书序列化程序文件
class BookSerializer < ActiveModel::Serializer
attributes :id, :title, :adult_content, :published
belongs_to :author
has_many :genres
end
测试样本数据
def setup
...
@fantasy = genres(:fantasy)
@newbook = {
title: "Three Little Pigs",
adult_content: false,
author_id: @jim.id,
published: false,
genres: [{title: 'Fantasy'}]
}
end
测试方法
test "book create - should create a new book" do
post books_path, params: @newbook, headers: user_authenticated_header(@jim)
assert_response :created
json = JSON.parse(response.body)
puts "json = #{json}"
assert_equal "Three Little Pigs", json['data']['attributes']['title']
genre_data = json['data']['relationships']['genres']['data']
puts "genre_data = #{genre_data.count}"
assert_equal "Fantasy", genre_data
end
预订强参数
def book_params
params.permit(:title, :adult_content, :published, :author_id, :genres)
end
测试结果(控制台响应)
# Running:
......................................................json = {"data"=>{"id"=>"1018350796", "type"=>"books", "attributes"=>{"title"=>"Three Little Pigs", "adult-content"=>false, "published"=>false}, "relationships"=>{"author"=>{"data"=>{"id"=>"1027431151", "type"=>"users"}}, "genres"=>{"data"=>[]}}}}
genre_data = 0
F
Failure:
BooksControllerTest#test_book_create_-_should_create_a_new_book [/Users/warlock/App_Projects/Raven Quill/Source Code/Rails/raven-quill-api/test/controllers/books_controller_test.rb:60]:
Expected: "Fantasy"
Actual: []
bin/rails test test/controllers/books_controller_test.rb:51
Finished in 1.071044s, 51.3518 runs/s, 65.3568 assertions/s.
55 runs, 70 assertions, 1 failures, 0 errors, 0 skips
正如您从我的 JSON 控制台日志中看到的那样,我的流派似乎没有被设置(需要在上面的测试输出中滚动到右侧)。
请忽略这一行:
assert_equal "Fantasy", genre_data
我知道那是错误的。目前,json 显示 genre => {data: []}
(空数组),这就是我目前要解决的问题。
在这种情况下,我该如何着手创作一本有流派的书,有什么想法吗? :D
这真是令人难过...这周第三次,我正在回答我自己的问题。
我终于从这个 Whosebug 问题中找到了答案:
HABTM association with Strong Parameters is not saving user in Rails 4
原来我的强参数需要是:
def book_params
params.permit(:title, :adult_content, :published, :author_id, {:genre_ids => []})
end
那么我的测试数据可以是:
@fantasy = genres(:fantasy)
@newbook = {
title: "Three Little Pigs",
adult_content: false,
author_id: @jim.id,
published: false,
genre_ids: [@fantasy.id]
}
将我的测试方法更新为:
test "book create - should create a new book" do
post books_path, params: @newbook, headers: user_authenticated_header(@jim)
assert_response :created
json = JSON.parse(response.body)
assert_equal "Three Little Pigs", json['data']['attributes']['title']
genre = json['data']['relationships']['genres']['data'].first['title']
assert_equal "Fantasy", genre
end
现在我的测试通过了。