Rails JSON_API使用类型列表创建Book

时间:2016-11-24 16:21:06

标签: ruby-on-rails json-api

我正在尝试编写测试,以确保创建一个新的book并为其分配genres

我正在使用带有JSON_API结构(http://jsonapi.org/

的Active Model Serializer

图书模型文件

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

Book Serializer文件

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

Book Strong Params

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

1 个答案:

答案 0 :(得分:0)

这只是悲伤...本周第三次,我正在回答我自己的问题。

我终于找到了Stackoverflow问题的答案:

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

现在我的考试通过了。