I'm doing a serializer for my application and i need to test it, but it always return 'json atom at path "name" is missing' when i run the specs. I'm using FastJson do build my serializer.
My StudentSerializer (all the attributes is in Students Model):
# frozen_string_literal: true
class Api::V2::StudentSerializer < ApplicationSerializer
attributes :email, :name, :school_id
end
My StudentSerializer_Spec:
require 'rails_helper'
RSpec.describe Api::V2::StudentSerializer, type: :serializer do
subject(:serializer) { described_class.new(student) }
context 'is student serialize working?' do
let(:student) { build_stubbed(:student) }
it 'serializes' do
Api::V2:: StudentSerializer.new (student)
expect(serializer).to include_json(
name: student.name
)
end
end
end
When i run the rspec, that's the result i get:
Api::V2::StudentSerializer
is student serialize working?
serializes (FAILED - 1)
1) Api::V2::StudentSerializer is student serialize working? serializes
Failure/Error:
expect(serializer).to include_json(
name: student.name
)
json atom at path "name" is missing
I figure it out!
By using FastJson, the Json start with 'data:':
{:data=>
{:id=>"1",
:attributes=>
{:email=>"",
:name=>"Burdette Schmeler",
:school_id=>1}}}
So, in my spec file, i need to put in this way:
context 'is student serialize working?' do
let(:student) { create(:student) }
it 'serializes' do
expect(serializer).to include_json(
data: {
id: student.id.to_s,
attributes: {
name: student.name,
email: student.email,
school_id: student.school_id,
}
}
)
Related
I have a book database where books can have different book formats (hardcover, softcover etc).
I have factories with factory_bot.
The following spec just run through with an error - and then when I run it the second time, it worked. I have no idea where I need to start searching....
The error was:
1) BookFormat displays the proper book format for a book with that format
Failure/Error: expect(#book.book_format.name).to eq('Hardcover')
expected: "Hardcover"
got: "Not defined"
Here is the full spec:
require 'rails_helper'
RSpec.describe BookFormat, type: :model do
before(:all) do
#book = create(:hobbit)
#book_format_default = create(:not_defined)
end
it 'displays the proper book format for a book with that format' do
expect(#book.book_format.name).to eq('Hardcover')
end
it 'should reassign to the fallback book_format if their book_format is deleted' do
format = #book.book_format
format.destroy
expect(#book.reload.book_format.id).to eq(#book_format_default.id)
end
it 'should not let the fallback format be deleted' do
format = #book_format_default
format.destroy
expect(format).to be_truthy
end
end
Here is the corresponding factor for the book :hobbit:
factory :hobbit, class: Book do
title { 'The Hobbit' }
year { 1937 }
rating { 5 }
condition { 4 }
synopsis { "<p>#{Faker::Lorem.paragraphs(number: 30).join(' ')}</p>" }
association :book_format, factory: :hardcover
association :user, factory: :me
genres { [ create(:fiction) ] }
after(:build) do |hobbit|
hobbit.cover.attach(
# rubocop:disable Rails/FilePath
io: File.open(Rails.root.join('db', 'sample', 'images', 'cover-1.jpg')),
# rubocop:enable Rails/FilePath
filename: 'cover.jpg',
content_type: 'image/jpeg'
)
end
end
And here are the factories for book_formats:
FactoryBot.define do
factory :not_defined, class: BookFormat do
name { 'Not defined'}
fallback { true }
end
factory :hardcover, class: BookFormat do
name { 'Hardcover' }
end
factory :softcover, class: BookFormat do
name { 'Softcover' }
end
end
I have spent a lot of time trying to emplement ability of validation incoming params is rswag specs, my code:
# incoming-parameter
params = {
login: 'www',
id: 15
}
# test rswag-spec
path '/controller/hello' do
post('Say Hello!') do
tags 'users'
consumes 'application/json'
produces 'application/json'
parameter name: :my_params, in: :body, schema: {
type: :object,
required: %i[id name],
properties: {
id: { type: :string },
name: { type: :string }
}
}
response(200, 'successful') do
# schema '$ref' => '#/components/schemas/UserRegistrationResponse'
describe 'new user with valid reg_params' do
let(:my_params) { params }
run_test! do |response|
data = JSON.parse(response.body)
puts "data = #{data}"
end
end
end
end
end
You expecting that incoming params won't pass validation, because id - is an integer, and name field is absent. But that's doesn't matter and test is compliting with success.
Can you say what's wrong with my code an why don't work validation of incoming parameters those declarated in rswag docs?
With RSwag and RSpec in Ruby on Rails, is it possible to send a parameter that is not defined as a parameter via the run_test!?
Example test:
# frozen_string_literal: true
require "swagger_helper"
describe "Resources", type: :request, swagger_doc: "api/resources.json" do
path "/resources" do
get "get resources" do
tags "resources"
parameter name: "sort[column]",
in: :query,
type: :string,
enum: ["created_at", "updated_at"]
required: false
parameter name: "sort[order]",
in: :query,
type: :string,
enum: ["asc", "desc"]
required: false
response "422", "Unprocessable Entity" do
context "with unrecognized param" do
# define sort[direction] here
run_test! do |respone|
expect(json_response["errors"]).to contain_exactly(
# my serialized response error
)
end
end
end
end
end
end
I'd like to send sort[direction] in my context. What I have tried so far:
let(:"sort[direction]") { "asc" }
let(:sort) { { direction: "asc" } }
let(:sort) { { "direction" => "asc" } }
with no avail - I got HTTP 200 Success, even though the same request sent via Postman with sort[direction] defined responds with expected HTTP 422 Unprocessable Entity.
I have managed to do it this way:
context "with unrecognized sort param" do
before do |example|
example.metadata[:example_group][:operation][:parameters] += [{
name: "sort[direction]",
in: :query,
type: :string,
required: false
}]
end
let(:"sort[direction]") { "asc" }
run_test! do |respone|
expect(json_response["errors"]).to contain_exactly(
# my serialized response error
)
end
end
It does not add the params to the generated OpenAPI JSON file, which is good.
I've inherited a rails api and I'm trying to test controllers. I have an endpoint '/api/v1/vitcords' where I create new vitcords. The video model only has a validation name. So my doubt is how to test that when I create a new video without specify a name, I get the message error I want, that in this case is "Vitcord name has to be specified". Thanks.
This is the Vcord model
class Vcord
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Spacial::Document
include Concerns::Vitcord::M3u8
include Concerns::Vitcord::User
# Validations
validates_presence_of :name
# Atributes
field :name, type: String
field :location, type: Array, spacial: true
field :address, type: String
field :closed, type: Boolean, default: false
field :public, type: Boolean, default: false
field :thumb, type: String
end
This is the controller video_controller.rb
module Api::V1
class VitcordsController < ApiController
def create
user = current_resource_owner
# Validation
param! :name, String, required: true
param! :location, Hash, required: false
param! :address, String, required: false
ap params[:location]
ap params[:location][:latitude]
ap params[:location][:longitude]
# Creating
vcord = Vcord.new()
vcord.name = params[:name] if params[:name]
if params[:location] && params[:location]['latitude'] && params[:location]['longitude']
vcord.location = {
lng: params[:location]['longitude'],
lat: params[:location]['latitude']
}
end
vcord.address = params[:address] if params[:address]
vcord.creator = user
if vcord.save
vcord.members.each do |member|
Notification.generate_notification_for_vitcord_invited(vcord, member)
end
#vitcord = vcord
else
error(:internal_server_error, ["Vitcord name has to be specified"], nil)
end
end
end
And this is the spec
require 'rails_helper'
describe "POST /api/v1/vitcords" do
before(:each) do
db_drop
post "/oauth/token", {
:grant_type => 'assertion',
:assertion => TOKEN
}
#token = response_json['access_token']
end
it "sends an error if a vitcord is created with name nil" do
header 'Authorization', "Bearer #{#token}"
post '/api/v1/vitcords', {
address: "Calle Rue del Percebe",
location: {
latitude: 40.7127837,
longitude: -74.00594130000002
}
}
//This does not work but it would be something like this
expect(error).to eq("Name has to be specified")
end
end
Well, you should refactor your code, but answering your question, you can add an error to you object by doing (look that I used #vcord and not vcord):
#vcord.errors.add(:name, 'Vitcord name has to be specified')
(as you can see here http://api.rubyonrails.org/classes/ActiveModel/Errors.html#method-i-add)
and on your test:
expect(assigns(:vcord).errors.name).to eq('Vitcord name has to be specified').
I would like to test my models but all informations that I could find seems to be outdated. My goal is to test each individual validation.
My model:
class Author < ActiveRecord::Base
has_and_belongs_to_many :books
before_save :capitalize_names
validates :name, :surname, presence: true, length: { minimum: 3 },
format: { with: /[a-zA-Z]/ }
private
def capitalize_names
self.name.capitalize!
self.surname.capitalize!
end
end
and my factorygirl define:
FactoryGirl.define do
factory :author do |f|
f.name { Faker::Name.first_name }
f.surname { Faker::Name.last_name }
end
end
So now, I want to test whether name is not shorter than 3 characters.
My context:
context 'when first name is too short' do
it { expect( FactoryGirl.build(:author, name: 'Le')).to
be_falsey }
end
I know it's invalid because of [FactoryGirl.build(:author, name: 'Le')] returns hash instead of boolean value. So now, how should I test it? What matcher should I use?
[SOLVED]
Use be_valid instead of be_falsey. Now it should look like :
context 'when first name is too short' do
it { expect( FactoryGirl.build(:author, name: 'Le')).not_to
be_valid }
end