Skip to content

Instantly share code, notes, and snippets.

@alexshagov
Created May 12, 2020 18:25
Show Gist options
  • Save alexshagov/f1d313c2c255e61773c1ef19fbb42a7b to your computer and use it in GitHub Desktop.
Save alexshagov/f1d313c2c255e61773c1ef19fbb42a7b to your computer and use it in GitHub Desktop.
User spec sample for students
require 'rails_helper'
RSpec.describe User, type: :model do
subject { described_class.new(params) }
let(:params) do
{
name: 'John'
}
end
describe 'validations' do
it { should validate_presence_of(:name) }
end
describe 'associations' do
it { should have_many(:books) }
end
describe '#read?' do
let(:user) { subject.save }
let(:book_title) { 'Book title' }
before do
user
# book
end
context 'when book has been read by a user' do
# let(:book) { FactoryBot.create(:book, user: subject,
# title: book_title, read: true) }
# it { expect(subject.read?(book_title)).to eq true }
let(:books_relation_double) { double }
before do
allow(subject).to receive(:books).and_return(books_relation_double)
end
it do
expect(books_relation_double).to receive(:exists?)
.with(title: book_title,
read: true)
.and_return(true)
expect(subject.read?(book_title)).to eq true
end
end
context 'when book has not been read by a user' do
let(:book) { FactoryBot.create(:book, user: subject,
title: book_title, read: false) }
it { expect(subject.read?(book_title)).to eq false }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment