How do you approach rspec 2 questions? I am stuck figuring what I am trying to solve

Juggling with a lot of new information, I am not sure how to approach rspec questions especially for performance_monitor. Where do I even start?

I think everyone has their own methods of getting thru the exercises, but personally I find it really helpful to check out the solutions if I’m feeling completely in the dark.

From there, I go through the solution and explain why each thing works the way it does to myself until I feel confident. Then I complete the exercise without looking back at the solution if possible.

Just a thought!

If you’re super stuck, it’s alright to take an occasional peek at solutions, as long as you’re doing what Laurette suggests: asking yourself what each piece is doing to get a deep understanding of the whole.

I strongly recommend looking at the actual spec files. It may look like gibberish right now, but specs are simple at their core. They’re simply calling your methods with different scenarios/inputs and testing that it behaves correctly. For performance_monitor, I believe it’s looking for a method called measure. Hopefully that’s enough to get you started!

It only helps with a part of that problem, but Beginning Ruby covers the Time class starting on page 66. As far as I understand, you can store the time at the start by assigning to a variable, then call the proc a certain number of times, then create another variable to store the end time. You can then use those start and end times to calculate the answer.

I definitely had to look at the solutions sometimes, too.

Can someone explain what the code in the triple brackets do in performance_monitor and possibly explain what a stub is? Thanks.

[[[describe “Performance Monitor” do
before do
@eleven_am = Time.parse(“2011-1-2 11:00:00”)

it “takes exactly 0 seconds to run an empty block (with stubs)” do
allow(Time).to receive(:now).and_return(@eleven_am)

it “takes exactly 1 minute to run a block that sleeps for 1 minute (with stubs)” do
fake_time = @eleven_am
allow(Time).to receive(:now) { fake_time } ]]]

elapsed_time = measure do
  fake_time += 60  # adds one minute to fake_time

expect(elapsed_time).to eq(60)


1 Like

A stub is simply an instruction in the spec file to call a method and return a given value. This is generally done to assist in testing code. Here, the spec calls the ‘now’ method on ‘Time’ and instructs it to return the instance variable @eleven_am.

1 Like