Skip to content

Instantly share code, notes, and snippets.

@zedtux
Last active March 7, 2018 13:31
Show Gist options
  • Save zedtux/6c53d9b304b26acd68c0cf4066ab6927 to your computer and use it in GitHub Desktop.
Save zedtux/6c53d9b304b26acd68c0cf4066ab6927 to your computer and use it in GitHub Desktop.
Unit tests for the simplest Rails API ever example with RSpec
require 'rails_helper'
RSpec.describe UsersController, type: :controller do
describe 'index' do
context 'without any user' do
before { get :index }
it { is_expected.to respond_with 200 }
it 'should return an empty list' do
expect(JSON.parse(response.body)).to eql([])
end
end
context 'with users' do
before do
my_group = Group.create!(name: 'Kaamelott', status: 1)
password = 'Arthur! Pas changer assiette pour fromage! @2005'
attributes = {
group: my_group,
status: 1,
password: password,
password_confirmation: password
}
@user1 = User.create!(
attributes.merge(
first_name: 'Guillame',
last_name: 'Briat',
email: '[email protected]'
)
)
@user2 = User.create!(
attributes.merge(
first_name: 'Alexandre',
last_name: 'Astier',
email: '[email protected]'
)
)
get :index
end
it { is_expected.to respond_with 200 }
it 'should return a list composed of users' do
expect(JSON.parse(response.body)).to eql(
[
{
'id' => @user1.id,
'created_at' => @user1.created_at,
'updated_at' => @user1.updated_at,
'first_name' => 'Guillaume',
'last_name' => 'Briat',
'status' => 1,
# ...
}
]
)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment