http://www.thinknetica.com
RSpec & Friends 2:
Parallel Tests
Controllers
Shared Groups
1
http://www.thinknetica.com
Parallel Tests
# Gemfile
group :development do
gem 'parallel_tests'
end
# Создаем и обновляем схему БД
rake parallel:create
rake parallel:prepare
# database.yml
test:
database: th_demo_test<%= ENV['TEST_ENV_NUMBER'] || ''%>
# Запускаем
DISABLE_SPRING=1 rake parallel:spec
2
http://www.thinknetica.com
Controllers
# Read/Delete methods
describe "GET #index" do
let(:params) { {} }
subject { get :index, params }
context "pagination" do
let(:params) { { page: 1, per_page: 2 } }
it "returns 2 items" do
expect(subject.body).to have_json_size(2).at_path("items")
end
end
end
3
http://www.thinknetica.com
Controllers
# Update methods
describe "POST #create" do
let(:form_params) { {} }
let(:params) do
{ format: :json, item: attributes_for(:item).merge(form_params) }
end
subject { post :create, params }
context "invalid params" do
let(:form_params) { { name: nil } }
it "doesn't create new item" do
expect { subject }.not_to change(Item, :count)
end
end
end
4
http://www.thinknetica.com
Shared Groups
5
Shared
Examples
Shared
Context
http://www.thinknetica.com
Shared Contexts
6
#./spec/shared_context/shared_users.rb
shared_context "users", users: true do
let(:user) { create(:user) }
let(:john) { create(:user, name: "John") }
let(:jack) { create(:user, name: "Jack") }
end
# подключение контекста
include_context "users"
# или с помощью тэгов
context "with users", :users do
# здесь нам доступны user, john и jack
end
http://www.thinknetica.com
 Примеры
7
shared_context "controller", type: :controller do
include_context "users"
end
shared_context "russian locale", ru: true do
around(:each) { |ex| I18n.with_locale(:ru, &ex) }
end
shared_context "english locale", en: true do
around(:each) { |ex| I18n.with_locale(:en, &ex) }
end
http://www.thinknetica.com
Shared Examples
8
# ./spec/shared_examples/valid_examples.rb
shared_examples "valid object" do
specify { is_expected.to be_valid }
end
#./spec/models/user_spec.rb
it_behaves_like "valid object"
# равносильно следующему коду
context "valid object" do
specify { is_expected.to be_valid }
end
http://www.thinknetica.com
 Примеры
9
shared_examples "invalid params" do |message, model: false, code:
403|
context message do
specify { expect(subject).to have_http_status(code) }
if model
specify { expect { subject }.not_to change(model, :count) }
end
end
end
include_examples "invalid params", "missing name", model: Item do
let(:form_params) { { name: nil } }
end

RSpec. Part 2