Class Interactions: Students & Courses

Whenever I run the /spec/student_spec.rb file, I get the following error message:

Double :new_course received unexpected message :add_student with (#<Student:0x007fe4539b1ee8 @first_name="Johnny", @last_name="Rocket", @course_list=[#<RSpec::Mocks::Double:0x3ff229cd8d08 @name=:old_course>, #<RSpec::Mocks::Double:0x3ff229cd88bc @name=:new_course>]>)

The relevant code I wrote in the /lib/student.rb is:

def enroll(course) return if @course_list.include?(course) @course_list.push(course) course.add_student(self) end

I defined the relevant instance method course#add_student(student) in the course.rb file, but for some reason the given course instance is not receiving the method call as expected.


Hey! Your error message says “Double :new_course received unexpected message :add_student”, which means that new_course isn’t supposed to have add_student called on it.

Hey, the wording was confusing for me so I looked it up on piazza. Here’s the hint from over there that helped. It’s basically just about reversing the roles of the classes, nothing wrong on your part just the way the test is written.

This threw me off for a bit too, but I will give you a massive clue (without hopefully ruining it for you):

If I remember correctly, you should be updating the student enrolled course list, and the course’s enrolled student list within the student class, right?

therefore your Course’s add_student method should do something like this
def add_student(student)
student.enroll(#call the current course)

and your student should not only update their enrolled_courses list, they should also update the course’s enrolled_students list. The latter can be done through student (given that it has a course object as a parameter), and you already provide access to the course’s list of enrolled students

1 Like


I’ve just had the same problem. I passed the specs by switching the responsibilities of the enroll method and add_student method as you have suggested. Now my enroll method looks like this:

def enroll(new_course)
courses << new_course unless courses.include?(new_course)
new_course.students << self unless new_course.students.include?(self)
# course_load[new_course.department] = 1
#new_course received unexpected message

And my add_student looks like this:

def add_student(student)
students << student unless students.include?(student)
# student.course_load[“hello”] = 1
#student received unexpected message

However, if you look at the commented lines, you can see that I’m having the same exact problem with course_load not updating. I’ve tried updating it from Student and from Course but it gives me the same error each time: student/new_course received unexpected message. Since this is just like the previous error, I thought I’d be able to figure it out but I can’t update course_load from either side. Any suggestions on where I’m supposed to do that?

I’ve figured it out.

Comparing course_load to courses was my mistake and the source of my frustration.

Courses is made to be updated through enroll which is why it can simply be manipulated with a simple courses << new_course command and has a 1 line definition. Meanwhile, course_load is NEVER updated unless it is called specifically.

Missing this small distinction caused me to treat them the same and be frustrated at why they weren’t behaving the same way. I WANTED to update course_load through enroll or add_student and wasted hours trying make one of these methods generate the hash when all I had to do was make the def of course_load generate the hash.

Hey Hein,

So I was having the same issue as you and fixed it using your method of creating a separate method for course_load. Mind explaining a bit further? Why doing something like
@course_load[course.department] += course.credits doesn’t work?

Hi Nate,

This is a good question.

I’m pretty sure this is mostly about the way that the specs are written, though I can’t be sure as I am an amateur just like most of the other students here.

The main errors in this topic were: “new_course received unexpected message” and “student received unexpected message”. I believe these messages aren’t saying there’s something wrong with the program, but rather that we are not writing it in a way that the tester intended us to. Obviously, there are many ways to achieve the same thing in programming and the tests are, generally speaking, looking for 1 specific way.

in response to Nate-

I dont know if you fixed this already, but I ran into the same problem too. Basically that line of code didnt work (initially) because you cant add to something that doesnt exist yet. At least thats what I think is happening.

I had actually initialized course_load as an empty hash in #initialize. And to Hein, I think you’re right.