[RUBY] What I stumbled upon in the ActiveModel :: Serializer test

Introduction

ActiveModel :: Serializer Rspec gave an example of how to write a test. However, I've stumbled upon writing a test for another Serializer, so I'll post it.

Source code

In this example, assume that Article and Category have a many-to-many relationship and have an intermediate model called ʻarticle_category.rb`.

article.rb


class Article < ApplicationRecord
  has_many :article_categories
  has_many :categories, through: :article_categories
end

article_serializer.rb


class ArticleSerializer < ActiveModel::Serializer
  attributes :id, :title
  has_many :categories
end

When I write the test of this Serializer, it looks like this

article_serializer_spec.rb


require 'rails_helper'

RSpec.describe ArticleSerializer, type: :serializer do
  context "When a new article is created" do
    let(:article) { create(:article) }
    let(:category) { create(:category) }
    let(:article_category) do
      create(:article_category, category: category, article: article)
    end

    it "Serialized JSON is created" do
      serializer = ArticleSerializer.new(article)
      expect(serializer.to_json).to eq(article.to_json(:only => [:id, :title], :include => { :category })
    end
  end
end

test results

When I tested it, the returned JSON didn't contain the has_many related category and was left empty. why,,,

Solution

It seems that the way this test was written was bad, so I wrote it like this and it passed.

article_serializer_spec.rb


require 'rails_helper'

RSpec.describe ArticleSerializer, type: :serializer do
  context "When a new article is created" do
    let!(:article) { create(:article) }
    let!(:category) { create(:category) }
    let!(:article_category) do
      create(:article_category, category: category, article: article)
    end

    it "Serialized JSON is created" do
      serializer = ArticleSerializer.new(article)
      expect(serializer.to_json).to eq(article.to_json(:only => [:id, :title], :include => { :category })
    end
  end
end

The change is that let is changed tolet!. If you leave let as it is, the only object created in the example is ʻarticle. The reason is that let` creates an object only when it appears in the example.

So it's a good idea to use let! In an example like this!

Recommended Posts

What I stumbled upon in the ActiveModel :: Serializer test
What I stumbled upon when installing Laravel-filemanager
[When using MiniMagick] A memorandum because I stumbled in the CircleCI test environment.
Note that I stumbled upon building the Rails environment
What I stumbled upon when installing Ruby on Ubuntu
[Rilas] What I learned in implementing the pagination function.
What if I write a finally clause in the try-with-resources syntax?
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
What are the rules in JUnit?
A note of what I stumbled upon and noticed in catching up with Laravel from Rails
I stumbled upon the development and publication of my first iPhone app
What I did when JSF couldn't display database information in the view
[Spring Boot] I stumbled upon a method call count test (Spock framework)
I get got errors: User must exist in the unit model test
What I investigated in Wagby development Note 1
I tried the new era in Java
What I learned through the swift teacher
I stumbled upon Netbeans + JDBC + Amazon Athena
I was addicted to the Spring-Batch test
I tried the AutoValue library in Intellij
What I got into @Transactional in Spring
[RSpec] What I got stuck when I wrote the mailer (password reset process) test
I stumbled on the Java version in Android Studio, so I will summarize it
I made a primality test program in Java
What is Pullback doing in The Composable Architecture
I rewrote the Rails tutorial test with RSpec
I tried to organize the session in Rails
I wrote a primality test program in Java
What I learned in Java (Part 2) What are variables?
I want to get the value in Ruby
I stumbled when I tried using neo4j in the jenv environment, so make a note