Skip to content

Commit

Permalink
Rspec 3 syntax in specs
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyalberto committed Jan 27, 2015
1 parent b87fa49 commit 5602d1c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
10 changes: 5 additions & 5 deletions spec/lib/migration/tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@

context 'db:migrate' do
it "creates the expected column" do
@adapter_without_lock.tables.should_not include("test_rake")
expect(@adapter_without_lock.tables).not_to include("test_rake")
ActiveRecord::Migrator.migrate("spec/fixtures/db/migrate")
@adapter_without_lock.tables.should include("test_rake")
expect(@adapter_without_lock.tables).to include("test_rake")
end
end

context 'when rolling back' do
before(:each) do
@adapter_without_lock.create_table :test_rake
@adapter_without_lock.tables.should include("test_rake")
expect(@adapter_without_lock.tables).to include("test_rake")
insert_version(20140108194650)
end

context 'db:rollback' do
it "drops the expected table" do
ActiveRecord::Migrator.rollback("spec/fixtures/db/migrate", 1)
@adapter_without_lock.tables.should_not include("test_rake")
expect(@adapter_without_lock.tables).not_to include("test_rake")
end
end

context 'db:migrate:down' do
it "drops the expected table" do
ActiveRecord::Migrator.run(:down, "spec/fixtures/db/migrate", 20140108194650)
@adapter_without_lock.tables.should_not include("test_rake")
expect(@adapter_without_lock.tables).not_to include("test_rake")
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe ActiveRecord::ConnectionAdapters::Mysql2AdapterWithoutLock do
context "#initialize" do
it "successfully instantiates a working adapter" do
ActiveRecord::ConnectionAdapters::Mysql2AdapterWithoutLock.new(@adapter).should be_active
expect(ActiveRecord::ConnectionAdapters::Mysql2AdapterWithoutLock.new(@adapter)).to be_active
end
end

Expand All @@ -12,24 +12,24 @@
context "with alter" do
let(:query) { "alter " }
it "adds ', LOCK=NONE'" do
@adapter_without_lock.lock_none_statement("alter ").should == ", LOCK=NONE"
expect(@adapter_without_lock.lock_none_statement("alter ")).to eq(", LOCK=NONE")
end
end
context "with drop index" do
let(:query) { "drop index " }
it "adds ' LOCK=NONE'" do
@adapter_without_lock.lock_none_statement("drop index ").should == " LOCK=NONE"
expect(@adapter_without_lock.lock_none_statement("drop index ")).to eq(" LOCK=NONE")
end
end
context "with create index" do
let(:query) { "create index " }
it "adds ' LOCK=NONE'" do
@adapter_without_lock.lock_none_statement("create index ").should == " LOCK=NONE"
expect(@adapter_without_lock.lock_none_statement("create index ")).to eq(" LOCK=NONE")
end
end
context "with a query with LOCK=NONE already there" do
it "doesn't add anything" do
@adapter_without_lock.lock_none_statement("alter LOCK=NONE ").should == ""
expect(@adapter_without_lock.lock_none_statement("alter LOCK=NONE ")).to eq("")
end
end
end
Expand All @@ -44,7 +44,7 @@
end

it "doesn't add anything to the request" do
@adapter_without_lock.lock_none_statement("alter ").should == ""
expect(@adapter_without_lock.lock_none_statement("alter ")).to eq("")
end
end
end
Expand All @@ -54,14 +54,14 @@
it "adds LOCK=NONE at the end of the query" do
comma = query =~ /alter /i ? "," : ""
expected_output = "#{query} #{comma} LOCK=NONE"
@adapter_without_lock.should_receive(:original_execute).with(expected_output, nil)
expect(@adapter_without_lock).to receive(:original_execute).with(expected_output, nil)
@adapter_without_lock.execute(query)
end
end

shared_examples_for "#execute that doesn't change the SQL" do
it "just passes the query to original_execute" do
@adapter_without_lock.should_receive(:original_execute).with(query, nil)
expect(@adapter_without_lock).to receive(:original_execute).with(query, nil)
@adapter_without_lock.execute(query)
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/lib/mysql_online_migrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@

context ".prepended" do
it "sets ActiveRecord::Base.mysql_online_migrations to true" do
ActiveRecord::Base.mysql_online_migrations.should be_truthy
expect(ActiveRecord::Base.mysql_online_migrations).to be_truthy
end
end

context "#connection" do
shared_examples_for "Mysql2AdapterWithoutLock created" do
it "memoizes an instance of Mysql2AdapterWithoutLock" do
ActiveRecord::ConnectionAdapters::Mysql2AdapterWithoutLock.should_receive(:new)
expect(ActiveRecord::ConnectionAdapters::Mysql2AdapterWithoutLock).to receive(:new)
.with(an_instance_of(ActiveRecord::ConnectionAdapters::Mysql2Adapter)).once.and_call_original
3.times { migration.connection }
end
end

context 'when migrating' do
it "returns an instance of Mysql2AdapterWithoutLock" do
migration.connection.should be_an_instance_of(ActiveRecord::ConnectionAdapters::Mysql2AdapterWithoutLock)
expect(migration.connection).to be_an_instance_of(ActiveRecord::ConnectionAdapters::Mysql2AdapterWithoutLock)
end

it_behaves_like "Mysql2AdapterWithoutLock created"
Expand All @@ -33,8 +33,8 @@

it "returns an instance of ActiveRecord::Migration::CommandRecorder" do
recorder_connection = migration.connection
recorder_connection.should be_an_instance_of(ActiveRecord::Migration::CommandRecorder)
recorder_connection.instance_variable_get(:@delegate).should be_an_instance_of(ActiveRecord::ConnectionAdapters::Mysql2AdapterWithoutLock)
expect(recorder_connection).to be_an_instance_of(ActiveRecord::Migration::CommandRecorder)
expect(recorder_connection.instance_variable_get(:@delegate)).to be_an_instance_of(ActiveRecord::ConnectionAdapters::Mysql2AdapterWithoutLock)
end

it_behaves_like "Mysql2AdapterWithoutLock created"
Expand All @@ -43,11 +43,11 @@

context "#with_lock" do
it "switches mysql_online_migrations flag to false and then back to original value after block execution" do
ActiveRecord::Base.mysql_online_migrations.should be_truthy
expect(ActiveRecord::Base.mysql_online_migrations).to be_truthy
migration.with_lock do
ActiveRecord::Base.mysql_online_migrations.should be_falsy
expect(ActiveRecord::Base.mysql_online_migrations).to be_falsy
end
ActiveRecord::Base.mysql_online_migrations.should be_truthy
expect(ActiveRecord::Base.mysql_online_migrations).to be_truthy
end
end
end
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
require 'support/shared_examples/migration'

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus

Expand Down
8 changes: 4 additions & 4 deletions spec/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ def execute_without_lock(statement)
end

def unstub_execute
@adapter.unstub(:execute)
allow(@adapter).to receive(:execute).and_call_original
end

def stub_adapter_without_lock
ActiveRecord::ConnectionAdapters::Mysql2AdapterWithoutLock.stub(:new).and_return(@adapter_without_lock)
allow(ActiveRecord::ConnectionAdapters::Mysql2AdapterWithoutLock).to receive(:new).and_return(@adapter_without_lock)
end

def stub_execute(adapter, original_method, method_to_call)
original_execute = adapter.method(original_method)

adapter.stub(original_method) do |sql|
allow(adapter).to receive(original_method) do |sql|
if sql =~ CATCH_STATEMENT_REGEX
send(method_to_call, sql.squeeze(' ').strip)
else
Expand Down Expand Up @@ -96,7 +96,7 @@ def setup
end

def set_ar_setting(value)
ActiveRecord::Base.stub(:mysql_online_migrations).and_return(value)
allow(ActiveRecord::Base).to receive(:mysql_online_migrations).and_return(value)
end

def teardown
Expand Down
8 changes: 4 additions & 4 deletions spec/support/shared_examples/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def staged_for_travis
raise e unless @rescue_statement_when_stubbed
end

@queries_received_by_regular_adapter.length.should > 0
@queries_received_by_regular_adapter.length.should == @queries_received_by_adapter_without_lock.length
expect(@queries_received_by_regular_adapter.length).to be > 0
expect(@queries_received_by_regular_adapter.length).to eq(@queries_received_by_adapter_without_lock.length)
@queries_received_by_regular_adapter.each_with_index do |query, index|
@queries_received_by_adapter_without_lock[index].should == add_lock_none(query, comma_before_lock_none)
expect(@queries_received_by_adapter_without_lock[index]).to eq(add_lock_none(query, comma_before_lock_none))
end
end
end
Expand All @@ -62,7 +62,7 @@ def staged_for_travis
begin
migration.migrate(:up)
rescue ActiveRecord::StatementInvalid => e
e.message.should =~ /LOCK=NONE is not supported/
expect(e.message).to match(/LOCK=NONE is not supported/)
end
rebuild_table
end
Expand Down

0 comments on commit 5602d1c

Please sign in to comment.