Skip to content

Commit

Permalink
spec: mocha => rspec-mocks for plugins
Browse files Browse the repository at this point in the history
Change-Id: Icc0d1d279544cb811c919ff593795e39ba67b16b
Reviewed-on: https://gerrit.instructure.com/119985
Tested-by: Jenkins
Reviewed-by: Simon Williams <[email protected]>
Product-Review: Cody Cutrer <[email protected]>
QA-Review: Cody Cutrer <[email protected]>
  • Loading branch information
ccutrer committed Jul 25, 2017
1 parent 57b7dcd commit f03eaf3
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ def common_std
})
end
before do
AcademicBenchmark.stubs(:config).returns({partner_id: "instructure", partner_key: "key"})
standards_mock = mock("standards")
standards_mock.stubs(:authority_tree).returns(
allow(AcademicBenchmark).to receive(:config).and_return({partner_id: "instructure", partner_key: "key"})
standards_mock = double("standards")
allow(standards_mock).to receive(:authority_tree).and_return(
AcademicBenchmarks::Standards::StandardsForest.new([standard_instance, standard_instance2, standard_instance3,
standard_instance4, standard_instance5]).consolidate_under_root(authority_instance)
)
AcademicBenchmarks::Api::Standards.stubs(:new).returns(standards_mock)
allow(AcademicBenchmarks::Api::Standards).to receive(:new).and_return(standards_mock)
@user = admin_user
end
subject(:converter) do
Expand All @@ -170,7 +170,7 @@ def common_std
describe '#export' do
context 'when content_migration settings are missing' do
before do
converter.stubs(:content_migration).returns(nil)
allow(converter).to receive(:content_migration).and_return(nil)
end

it 'raises error missing content_migration settings' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ def run_and_check
end

it "should use the API to get the set data with an authority short code" do
response = Object.new
response.stubs(:body).returns(File.read(@level_0_browse))
response.stubs(:code).returns("200")
AcademicBenchmark::Api.expects(:get_url).with("http://example.com/browse?api_key=oioioi&authority=CC&format=json&levels=3").returns(response)
response = double()
allow(response).to receive(:body).and_return(File.read(@level_0_browse))
allow(response).to receive(:code).and_return("200")
expect(AcademicBenchmark::Api).to receive(:get_url).with("http://example.com/browse?api_key=oioioi&authority=CC&format=json&levels=3").and_return(response)

run_and_check
verify_full_import
Expand All @@ -237,28 +237,28 @@ def run_and_check
it "should use the API to get the set data with a guid" do
@cm.migration_settings[:authorities] = nil
@cm.migration_settings[:guids] = ["aaaaaaaaaa"]
response = Object.new
response.stubs(:body).returns(File.read(@a_levels_3))
response.stubs(:code).returns("200")
AcademicBenchmark::Api.expects(:get_url).with("http://example.com/browse?api_key=oioioi&format=json&guid=aaaaaaaaaa&levels=3").returns(response)
responsed = Object.new
responsed.stubs(:body).returns(File.read(@d_levels_3))
responsed.stubs(:code).returns("200")
AcademicBenchmark::Api.expects(:get_url).with("http://example.com/browse?api_key=oioioi&format=json&guid=ddddddddd&levels=3").returns(responsed)
responsej = Object.new
responsej.stubs(:body).returns(File.read(@j_levels_3))
responsej.stubs(:code).returns("200")
AcademicBenchmark::Api.expects(:get_url).with("http://example.com/browse?api_key=oioioi&format=json&guid=jjjjjjjjjjj&levels=3").returns(responsej)
response = double('a_levels_3')
allow(response).to receive(:body).and_return(File.read(@a_levels_3))
allow(response).to receive(:code).and_return("200")
expect(AcademicBenchmark::Api).to receive(:get_url).with("http://example.com/browse?api_key=oioioi&format=json&guid=aaaaaaaaaa&levels=3").and_return(response)
responsed = double('d_levels_3')
allow(responsed).to receive(:body).and_return(File.read(@d_levels_3))
allow(responsed).to receive(:code).and_return("200")
expect(AcademicBenchmark::Api).to receive(:get_url).with("http://example.com/browse?api_key=oioioi&format=json&guid=ddddddddd&levels=3").and_return(responsed)
responsej = double('j_levels_3')
allow(responsej).to receive(:body).and_return(File.read(@j_levels_3))
allow(responsej).to receive(:code).and_return("200")
expect(AcademicBenchmark::Api).to receive(:get_url).with("http://example.com/browse?api_key=oioioi&format=json&guid=jjjjjjjjjjj&levels=3").and_return(responsej)

run_and_check
verify_full_import
end

it "should warn when api returns non-success" do
response = Object.new
response.stubs(:body).returns(%{{"status":"fail","ab_err":{"msg":"API key access violation.","code":"401"}}})
response.stubs(:code).returns("200")
AcademicBenchmark::Api.expects(:get_url).with("http://example.com/browse?api_key=oioioi&authority=CC&format=json&levels=3").returns(response)
response = double()
allow(response).to receive(:body).and_return(%{{"status":"fail","ab_err":{"msg":"API key access violation.","code":"401"}}})
allow(response).to receive(:code).and_return("200")
expect(AcademicBenchmark::Api).to receive(:get_url).with("http://example.com/browse?api_key=oioioi&authority=CC&format=json&levels=3").and_return(response)

@cm.export_content
run_jobs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ def check_built_outcome(outcome)
end

it "raises error when crendentials are not set" do
AcademicBenchmark.stubs(:config).returns({})
allow(AcademicBenchmark).to receive(:config).and_return({})
expect{ AcademicBenchmark.ensure_ab_credentials }.to raise_error(Canvas::Migration::Error,
"Not importing academic benchmark data because the Academic Benchmarks Partner ID is not set")
AcademicBenchmark.stubs(:config).returns({partner_id: "user"})
allow(AcademicBenchmark).to receive(:config).and_return({partner_id: "user"})
expect{ AcademicBenchmark.ensure_ab_credentials }.to raise_error(Canvas::Migration::Error,
"Not importing academic benchmark data because the Academic Benchmarks Partner key is not set")
end
Expand Down
8 changes: 4 additions & 4 deletions gems/plugins/academic_benchmark/spec_canvas/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
end

def mock_api_call(code, body, url)
response = Object.new
response.stubs(:body).returns(body)
response.stubs(:code).returns(code.to_s)
AcademicBenchmark::Api.expects(:get_url).with(url).returns(response)
response = double()
allow(response).to receive(:body).and_return(body)
allow(response).to receive(:code).and_return(code.to_s)
expect(AcademicBenchmark::Api).to receive(:get_url).with(url).and_return(response)
end

it "should fail with bad AB response" do
Expand Down
14 changes: 7 additions & 7 deletions gems/plugins/account_reports/spec_canvas/report_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def initialize(account_report)

describe "#send_report" do
before do
AccountReports.stubs(available_reports: {account_report.report_type => {title: 'test_report'}})
report.stubs(:report_title).returns('TitleReport')
allow(AccountReports).to receive(:available_reports).and_return(account_report.report_type => {title: 'test_report'})
allow(report).to receive(:report_title).and_return('TitleReport')
end

it "Should not break for nil parameters" do
AccountReports.expects(:message_recipient)
expect(AccountReports).to receive(:message_recipient)
report.send_report
end

Expand Down Expand Up @@ -110,15 +110,15 @@ def initialize(account_report)
it 'should add course scope if course is set' do
courses = Course.all

report.stubs(:course).returns(@course3)
allow(report).to receive(:course).and_return(@course3)
courses = report.add_course_scope(courses)
expect(courses.count(:all)).to eq(1)
end

it 'should not add course scope if course is not set' do
courses = Course.all

report.stubs(:course).returns(nil)
allow(report).to receive(:course).and_return(nil)
courses = report.add_course_scope(courses)
expect(courses.count(:all)).to eq(3)
end
Expand All @@ -129,15 +129,15 @@ def initialize(account_report)
it 'should add term scope if term is set' do
courses = Course.all

report.stubs(:term).returns(@enrollment_term)
allow(report).to receive(:term).and_return(@enrollment_term)
courses = report.add_term_scope(courses)
expect(courses.count(:all)).to eq(2)
end

it 'should not add term scope if term is not set' do
courses = Course.all

report.stubs(:term).returns(nil)
allow(report).to receive(:term).and_return(nil)
courses = report.add_term_scope(courses)
expect(courses.count(:all)).to eq(3)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,8 @@ def create_some_group_memberships_n_stuff()
@user = user_with_managed_pseudonym(active_all: true, account: @root, name: 'Jimmy John',
username: '[email protected]', sis_user_id: 'other_shard')
end
@account.stubs(:trusted_account_ids).returns([@account.id, @root.id])
@account.stubs(:trust_exists?).returns(true)
allow(@account).to receive(:trusted_account_ids).and_return([@account.id, @root.id])
allow(@account).to receive(:trust_exists?).and_return(true)
@course1.enroll_user(@user)

parameters = {}
Expand Down Expand Up @@ -1198,7 +1198,7 @@ def create_some_group_memberships_n_stuff()
@uo1 = @user2.user_observees.create_or_restore(user_id: @user1)
uo2 = @user4.user_observees.create_or_restore(user_id: @user3)
@user7.user_observees.create_or_restore(user_id: @user6)
UserObserver.where(id: [@uo1.id, uo2.id]).update_all(sis_batch_id: @sis)
UserObserver.where(id: [@uo1.id, uo2.id]).update_all(sis_batch_id: @sis.id)
end

it 'should run user_observer provisioning report' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
end

it "should convert the assessment into a quiz" do
Qti::AssessmentTestConverter.any_instance.stubs(:unique_local_id).returns("random")
allow_any_instance_of(Qti::AssessmentTestConverter).to receive(:unique_local_id).and_return("random")
expect(get_quiz_data(d2l_question_dir, 'assessment', @opts).last.first).to eq D2LExpected::ASSESSMENT
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ def mock_migration.export_content
self.workflow_state = 'imported'
self.migration_settings[:imported_assets] = ["quizzes:quiz_xyz"]
end
ContentMigration.stubs(:new).returns(mock_migration)
ContentMigration.stubs(:find).with(mock_migration.id).returns(mock_migration)
allow(ContentMigration).to receive(:new).and_return(mock_migration)
allow(ContentMigration).to receive(:find).with(mock_migration.id).and_return(mock_migration)

status, details, context, item_id = soap_request(
'PublishServerItem', '[email protected]', 'asdfasdf', context,
Expand All @@ -279,8 +279,8 @@ def mock_migration.export_content
def @mock_migration.export_content
self.workflow_state = 'importing'
end
ContentMigration.stubs(:new).returns(@mock_migration)
ContentMigration.stubs(:find).with(@mock_migration.id).returns(@mock_migration)
allow(ContentMigration).to receive(:new).and_return(@mock_migration)
allow(ContentMigration).to receive(:find).with(@mock_migration.id).and_return(@mock_migration)

status, details, context, item_id = soap_request(
'PublishServerItem', '[email protected]', 'asdfasdf', context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Woozel < ActiveRecord::Base
woozel.with_versioning(&:save!)
expect(woozel.versions.loaded?).to eq false
first = woozel.versions.first
Woozel.connection.expects(:select_all).never
expect(Woozel.connection).to receive(:select_all).never
expect(first.versionable).to eq woozel
end

Expand All @@ -94,7 +94,7 @@ class Woozel < ActiveRecord::Base
woozel.with_versioning(&:save!)
expect(woozel.versions.loaded?).to eq false
all = woozel.versions.to_a
Woozel.connection.expects(:select_all).never
expect(Woozel.connection).to receive(:select_all).never
all.each do |version|
expect(version.versionable).to eq woozel
end
Expand Down

0 comments on commit f03eaf3

Please sign in to comment.