Skip to content

Commit 894ff18

Browse files
committed
change course#named_scopes specs to be more robust
fixes #CNVS-7154 Change-Id: I04a015442692332a1eb4fc3a82d490e3b793d862 Reviewed-on: https://gerrit.instructure.com/22707 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Jeremy Stanley <jeremy@instructure.com> Product-Review: Jeremy Stanley <jeremy@instructure.com> QA-Review: Bryan Madsen <bryan@instructure.com>
1 parent b6d60a3 commit 894ff18

1 file changed

Lines changed: 34 additions & 31 deletions

File tree

spec/models/course_spec.rb

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3217,128 +3217,131 @@ def new_external_tool(context)
32173217
context "named scopes" do
32183218
context "enrollments" do
32193219
before do
3220+
account_model
32203221
# has enrollments
3221-
@course1a = course_with_student(:course_name => 'A').course
3222-
@course1b = course_with_student(:course_name => 'B').course
3222+
@course1a = course_with_student(:account => @account, :course_name => 'A').course
3223+
@course1b = course_with_student(:account => @account, :course_name => 'B').course
32233224

32243225
# has no enrollments
3225-
@course2a = Course.create!(:name => 'A')
3226-
@course2b = Course.create!(:name => 'B')
3226+
@course2a = Course.create!(:account => @account, :name => 'A')
3227+
@course2b = Course.create!(:account => @account, :name => 'B')
32273228
end
32283229

32293230
describe "#with_enrollments" do
32303231
it "should include courses with enrollments" do
3231-
Course.with_enrollments.sort_by(&:id).should == [@course1a, @course1b]
3232+
@account.courses.with_enrollments.sort_by(&:id).should == [@course1a, @course1b]
32323233
end
32333234

32343235
it "should play nice with other scopes" do
3235-
Course.with_enrollments.where(:name => 'A').should == [@course1a]
3236+
@account.courses.with_enrollments.where(:name => 'A').should == [@course1a]
32363237
end
32373238

32383239
it "should be disjoint with #without_enrollments" do
3239-
Course.with_enrollments.without_enrollments.should be_empty
3240+
@account.courses.with_enrollments.without_enrollments.should be_empty
32403241
end
32413242
end
32423243

32433244
describe "#without_enrollments" do
32443245
it "should include courses without enrollments" do
3245-
Course.without_enrollments.sort_by(&:id).should == [@course2a, @course2b]
3246+
@account.courses.without_enrollments.sort_by(&:id).should == [@course2a, @course2b]
32463247
end
32473248

32483249
it "should play nice with other scopes" do
3249-
Course.without_enrollments.where(:name => 'A').should == [@course2a]
3250+
@account.courses.without_enrollments.where(:name => 'A').should == [@course2a]
32503251
end
32513252
end
32523253
end
32533254

32543255
context "completion" do
32553256
before do
3257+
account_model
32563258
# non-concluded
3257-
@c1 = Course.create!
3258-
@c2 = Course.create! :conclude_at => 1.week.from_now
3259+
@c1 = Course.create!(:account => @account)
3260+
@c2 = Course.create!(:account => @account, :conclude_at => 1.week.from_now)
32593261

32603262
# concluded in various ways
3261-
@c3 = Course.create! :conclude_at => 1.week.ago
3262-
@c4 = Course.create!
3263+
@c3 = Course.create!(:account => @account, :conclude_at => 1.week.ago)
3264+
@c4 = Course.create!(:account => @account)
32633265
term = @c4.account.enrollment_terms.create! :end_at => 2.weeks.ago
32643266
@c4.enrollment_term = term
32653267
@c4.save!
3266-
@c5 = Course.create!
3268+
@c5 = Course.create!(:account => @account)
32673269
@c5.complete!
32683270
end
32693271

32703272
describe "#completed" do
32713273
it "should include completed courses" do
3272-
Course.completed.sort_by(&:id).should == [@c3, @c4, @c5]
3274+
@account.courses.completed.sort_by(&:id).should == [@c3, @c4, @c5]
32733275
end
32743276

32753277
it "should play nice with other scopes" do
3276-
Course.completed.where(:conclude_at => nil).should == [@c4]
3278+
@account.courses.completed.where(:conclude_at => nil).should == [@c4]
32773279
end
32783280

32793281
it "should be disjoint with #not_completed" do
3280-
Course.completed.not_completed.should be_empty
3282+
@account.courses.completed.not_completed.should be_empty
32813283
end
32823284
end
32833285

32843286
describe "#not_completed" do
32853287
it "should include non-completed courses" do
3286-
Course.not_completed.sort_by(&:id).should == [@c1, @c2]
3288+
@account.courses.not_completed.sort_by(&:id).should == [@c1, @c2]
32873289
end
32883290

32893291
it "should play nice with other scopes" do
3290-
Course.not_completed.where(:conclude_at => nil).should == [@c1]
3292+
@account.courses.not_completed.where(:conclude_at => nil).should == [@c1]
32913293
end
32923294
end
32933295
end
32943296

32953297
describe "#by_teachers" do
32963298
before do
3297-
@course1a = course_with_teacher(:name => "teacher A's first course").course
3299+
account_model
3300+
@course1a = course_with_teacher(:account => @account, :name => "teacher A's first course").course
32983301
@teacherA = @teacher
3299-
@course1b = course_with_teacher(:name => "teacher A's second course", :user => @teacherA).course
3300-
@course2 = course_with_teacher(:name => "teacher B's course").course
3302+
@course1b = course_with_teacher(:account => @account, :name => "teacher A's second course", :user => @teacherA).course
3303+
@course2 = course_with_teacher(:account => @account, :name => "teacher B's course").course
33013304
@teacherB = @teacher
3302-
@course3 = course_with_teacher(:name => "teacher C's course").course
3305+
@course3 = course_with_teacher(:account => @account, :name => "teacher C's course").course
33033306
@teacherC = @teacher
33043307
end
33053308

33063309
it "should filter courses by teacher" do
3307-
Course.by_teachers([@teacherA.id]).sort_by(&:id).should == [@course1a, @course1b]
3310+
@account.courses.by_teachers([@teacherA.id]).sort_by(&:id).should == [@course1a, @course1b]
33083311
end
33093312

33103313
it "should support multiple teachers" do
3311-
Course.by_teachers([@teacherB.id, @teacherC.id]).sort_by(&:id).should == [@course2, @course3]
3314+
@account.courses.by_teachers([@teacherB.id, @teacherC.id]).sort_by(&:id).should == [@course2, @course3]
33123315
end
33133316

33143317
it "should work with an empty array" do
3315-
Course.by_teachers([]).should be_empty
3318+
@account.courses.by_teachers([]).should be_empty
33163319
end
33173320

33183321
it "should not follow student enrollments" do
33193322
@course3.enroll_student(user_model)
3320-
Course.by_teachers([@user.id]).should be_empty
3323+
@account.courses.by_teachers([@user.id]).should be_empty
33213324
end
33223325

33233326
it "should not follow deleted enrollments" do
33243327
@teacherC.enrollments.each { |e| e.destroy }
3325-
Course.by_teachers([@teacherB.id, @teacherC.id]).sort_by(&:id).should == [@course2]
3328+
@account.courses.by_teachers([@teacherB.id, @teacherC.id]).sort_by(&:id).should == [@course2]
33263329
end
33273330

33283331
it "should return no results when the user is not enrolled in the course" do
33293332
user_model
3330-
Course.by_teachers([@user.id]).should be_empty
3333+
@account.courses.by_teachers([@user.id]).should be_empty
33313334
end
33323335

33333336
it "should play nice with other scopes" do
33343337
@course1a.complete!
3335-
Course.by_teachers([@teacherA.id]).completed.should == [@course1a]
3338+
@account.courses.by_teachers([@teacherA.id]).completed.should == [@course1a]
33363339
end
33373340
end
33383341

33393342
describe "#by_associated_accounts" do
33403343
before do
3341-
@root_account = Account.default
3344+
@root_account = account_model
33423345
@sub = account_model(:name => 'sub', :parent_account => @root_account, :root_account => @root_account)
33433346
@subA = account_model(:name => 'subA', :parent_account => @sub1, :root_account => @root_account)
33443347
@courseA1 = course_model(:account => @subA, :name => 'A1')

0 commit comments

Comments
 (0)