首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails JSON_API创建带有类型列表的图书

Rails JSON_API创建带有类型列表的图书
EN

Stack Overflow用户
提问于 2016-11-24 16:21:05
回答 1查看 235关注 0票数 1

我正在尝试编写我的测试,以确保创建一个新的book,并将genres分配给它。

我使用的是带有JSON_API结构(http://jsonapi.org/)的活动模型序列化程序

图书模型文件

代码语言:javascript
复制
class Book < ApplicationRecord
  belongs_to :author, class_name: "User"
  has_and_belongs_to_many :genres
end

体裁模型文件

代码语言:javascript
复制
class Genre < ApplicationRecord
  has_and_belongs_to_many :books
end

图书串行化文件

代码语言:javascript
复制
class BookSerializer < ActiveModel::Serializer
  attributes :id, :title, :adult_content, :published

  belongs_to :author
  has_many :genres
end

测试样本数据

代码语言:javascript
复制
def setup
    ...
    @fantasy = genres(:fantasy)

    @newbook = {
      title: "Three Little Pigs",
      adult_content: false,
      author_id: @jim.id,
      published: false,
      genres: [{title: 'Fantasy'}]
    }
end

测试方法

代码语言:javascript
复制
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

书中的强平行曲

代码语言:javascript
复制
def book_params
    params.permit(:title, :adult_content, :published, :author_id, :genres)
end

测试结果(控制台响应)

代码语言:javascript
复制
# 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控制台日志中可以看到,我的类型似乎没有被设置(需要在上面的测试输出中向右滚动)。

请忽略这一行:

代码语言:javascript
复制
assert_equal "Fantasy", genre_data

我知道这是不对的。目前,json正在显示genre => {data: []} (空数组),这是我目前正试图解决的问题。

在这种情况下,我如何创建一本有体裁的书,有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-24 16:50:58

本周正好是sad...third时间,我正在回答我自己的问题。

我终于从这个堆栈溢出问题中找到了答案:

HABTM association with Strong Parameters is not saving user in Rails 4

原来我的强参数应该是:

代码语言:javascript
复制
def book_params
    params.permit(:title, :adult_content, :published, :author_id, {:genre_ids => []})
end

那么我的测试数据可以是:

代码语言:javascript
复制
@fantasy = genres(:fantasy)

@newbook = {
  title: "Three Little Pigs",
  adult_content: false,
  author_id: @jim.id,
  published: false,
  genre_ids: [@fantasy.id]
}

更新我的测试方法以:

代码语言:javascript
复制
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

现在我的测试通过了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40790997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档