#ruby, #rails, #OOP, #javascript, #ember

1/9/16

Great expect(ations) w RSpec

The holidays are over an I have been chipping away daily. This week I have dedicated much of my time to learning RSpec and testing ruby frameworks. Searching around there isnt an abundance of testing information for beginners on the web, but I signed up for a free trial at Lynda.com and have been very happy with there 6hr class on testing with RSpec. I'm about 3/4 finished and have learned much of the syntax and theory behind RSpec.

If your learning Rails you should learning some sort of testing. It may not be necessary in the beginning, but you will need to have it in your tool belt when things get a bit more complex. Also, I feel that if you wait too long to learn testing it could become a bottle neck in your growing progress.

Also, I can't recommend Lynda.com enough. I have only viewed their course on testing ruby frameworks but I have a feeling that everything they do is at a high level. I plan to continue to use them as I move forward.

Here is a summary of what I have learned the past two days with Lynda and RSpec:

The hiearchy is important: spec file => example groupe => nested group => example => expectations is really the meat of the whole operation and once I understood that I really started to understand the big picture.

Helper methods: It is important to keep your suite as fast as possible therefore encouraging frequent tests. Now this is a non issue for small projects, but as things grow I could see how this would slow things up. Helper methods help keep unnecessary tests from running and slowing down the process.

The let method is really cool because it creates a helper method that takes one argument, which is the name of the method you want to create and then a block of code.

ex.
let(:car) { Car.new}

it "allows reading for :wheels" do
  expect(car.wheels).to eq(4)
end

you can also use subject as your helper

ex.
subject {Car.new}
  it ....
end

........and going farther you can define subjects implicitly by using a class name as the 'describe' argument

ex.
(this is how it looked like before)
describe 'Car'
  subject { Car.new }
  it ....
  end
end

*is the same thing as

describe Car
    it...
    end
end

^Here the subject is implicitly defined. As soon as it encounters the class as an argument it creates a helper method with the subject as the class Car. this works well if the subject you are working with is in only one class.

For Rails testing I learned about SHARED EXAMPLES, which is where RSpec begins to get more complex.

Shared examples let you describe behaviour of classes or modules. When declared,
a shared group's content is stored. It is only realized in the context of
another example group, which provides any context the shared group needs to
run.

In rails this comes in handy when describing things like 'CRUD' in your controller.

ex.
describe ProductsController do
  describe '#index'
    it_behaves_like 'a standard index action'
  end
  describe '#new'
    it_behaves_like 'a standard index action'
  end
end

.....and so on

Should be finishing up the rest of the course tomorrow along with the last touches on my portfolio and resume. I'll be going to a meetup this next Thursday at Hashrocket in Jax Beach, and they give opportunities at the end for attendees to put projects up in front of the group to have it "torn a apart" by the pros. I hope to have a TDD or BDD app built from the ground up by then; I'd love to get live feedback that way.

it is now 4 a.m., please excuse the typos

-John

No comments: