discourse/spec/controllers/admin/badges_controller_spec.rb
Andy Waite 3e50313fdc Prepare for separation of RSpec helper files
Since rspec-rails 3, the default installation creates two helper files:
* `spec_helper.rb`
* `rails_helper.rb`

`spec_helper.rb` is intended as a way of running specs that do not
require Rails, whereas `rails_helper.rb` loads Rails (as Discourse's
current `spec_helper.rb` does).

For more information:

https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files

In this commit, I've simply replaced all instances of `spec_helper` with
`rails_helper`, and renamed the original `spec_helper.rb`.

This brings the Discourse project closer to the standard usage of RSpec
in a Rails app.

At present, every spec relies on loading Rails, but there are likely
many that don't need to. In a future pull request, I hope to introduce a
separate, minimal `spec_helper.rb` which can be used in tests which
don't rely on Rails.
2015-12-01 20:39:42 +00:00

77 lines
2.1 KiB
Ruby

require 'rails_helper'
describe Admin::BadgesController do
context "while logged in as an admin" do
let!(:user) { log_in(:admin) }
let!(:badge) { Fabricate(:badge) }
context 'index' do
it 'returns badge index' do
xhr :get, :index
expect(response).to be_success
end
end
context '.save_badge_groupings' do
it 'can save badge groupings' do
groupings = BadgeGrouping.all.order(:position).to_a
groupings << BadgeGrouping.new(name: 'Test 1')
groupings << BadgeGrouping.new(name: 'Test 2')
groupings.shuffle!
names = groupings.map{|g| g.name}
ids = groupings.map{|g| g.id.to_s}
xhr :post, :save_badge_groupings, ids: ids, names: names
groupings2 = BadgeGrouping.all.order(:position).to_a
expect(groupings2.map{|g| g.name}).to eq(names)
expect((groupings.map(&:id) - groupings2.map{|g| g.id}).compact).to be_blank
expect(::JSON.parse(response.body)["badge_groupings"].length).to eq(groupings2.length)
end
end
context '.badge_types' do
it 'returns success' do
xhr :get, :badge_types
expect(response).to be_success
end
it 'returns JSON' do
xhr :get, :badge_types
expect(::JSON.parse(response.body)["badge_types"]).to be_present
end
end
context '.destroy' do
it 'returns success' do
xhr :delete, :destroy, id: badge.id
expect(response).to be_success
end
it 'deletes the badge' do
xhr :delete, :destroy, id: badge.id
expect(Badge.where(id: badge.id).count).to eq(0)
end
end
context '.update' do
it 'returns success' do
xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false, multiple_grant: false, enabled: true
expect(response).to be_success
end
it 'updates the badge' do
xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false, multiple_grant: true, enabled: true
expect(badge.reload.name).to eq('123456')
end
end
end
end