RSPEC Tutorials?

Is there some RSPEC material in the Alpha course that I just missed somehow? The coursework only covers (barely) how to install RSPEC, and doesn’t seem to explain what RSPEC is or how it works.

With that in mind, I think I am going to spend today doing some third-party RSPEC tutorials and then come back to the Alpha stuff. The two below-linked ones look promising:


I will keep you posted.

I was similarly confused at first. I have a Lynda membership through my local public library so yesterday while running some errands I listened to the first chunk of this course, and that got me confident enough to dive into the exercises for the RSPEC unit.

Once I started, it turns out they’re pretty fun and self-explanatory. Just open each spec file alongside the corresponding ruby file and write the code required by each spec (and then the necessary tests for the extra exercises). I think what’s missing in the RSPEC unit is a brief note saying that the approach for this section is different from the others. Maybe there was such a note, but if so I missed it.

Hi E -
That Lynda course looks pretty good…the first part looks a bit more basic than what I need right now, but I’ll hopefully have time soon to set up a Lynda membershp and check it out in more depth.

I actually skipped several of the exercises in the RSPEC folder that seemed to require knowledge of stuff (Class objects and interactions) that hadn’t been covered in the course yet, so I can’t comment on that directly.

At the moment, though, I’m doing the “Students and Courses” exercise from the “Class Interactions” section and my problem is that my code runs fine in a REPL (and seems to satisfy the description in English in the spec file)-- but RSPEC throws errors on the same code and I just don’t know enough about RSPEC to understand what those errors mean.

Any thoughts?

Could you paste the relevant portions of the RSPEC error text? I haven’t gotten to that exercise yet but it would be fun to help figure it out.

Pasting just the error wouldn’t really help much in this case, I think…to provide sufficient context, I’d have to post my entire solution as well. But if you want to be helpful: did you manage to solve the “Performance Monitor” exercise in RSPEC-2—and how did you see the solution? I was completely gobsmacked by the spec reproduced below. I had to ask the ever-helpful Kevin from AA, who gave me a very clear and concise solution…but I think I’d still need to look at my notes if I had to solve it again right now.

 it "takes about 0 seconds to run an empty block" do
    elapsed_time = measure { }

    expect(elapsed_time).to be_within(0.1).of(0)
  end

Here’s my thought process for that particular test. At first I was confused about what exactly was being tested, because the test doesn’t have the same structure as the ones in previous exercises. So I ran the spec without doing anything just to see what the error message was when it failed, and it pointed to a nonexistent method “measure”, which made clear that measure was the method I needed to be writing here, which makes sense.

Next I looked up the docs for Time, figured out the basics of how it works, and did what was necessary for “elapsed_time” (which receives the return value from “measure”) to be set equal to the difference in times before and after measure yields to the block. That was enough to satisfy this particular test.

Edit: I should mention that the most confusing thing about “Performance Monitor” was the introduction of stubs without any context or explanation. The exercise itself was really easy, but I didn’t initially use Time::now, and was kind of dumbfounded by the failure of my solution until I grasped that the spec was requiring me to use it.

Eeeyeah. It’s still not quite clear to me what stubs are useful for (the RSPEC documentation is short on specific examples of their use) but I managed to pass that spec anyway. My difficulties with the exercise were 1) I had no idea you could define a variable (elapsed_time = x) inside a spec file and 2) there actually exists a built-in “measure” method in Ruby (https://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html) and I assumed that was what was needed—which led me right off into the wilderness.

My brain is really not having an easy time encompassing the gestalt of RSPEC, but I suppose there’s nothing to be done for it but to forge ahead. Onward.

1 Like