May 12 Ruby Nation: Lots of RSpec syntax
tags:
For a week or two I was happily pleased with Jon Lark Larkowski RSpec presentation at Ruby Nation on RSpec syntax, and was eager to use that and other recommendations to improve my specs. Well it was time to test a helper in one of my Rails apps. Check it out, it is sweet.
require 'spec_helper' # you know everything you need is handled in your spec helper
describe ProfilesHelper do
# I build myself a class that helps me test my helper
# and check there is no instance variable here
let(:profiles_helper) do
class ProfilesHelperSpecer
include ProfilesHelper
end.new
end
# describing an instance method
describe '#custom_company_type?' do
# in a given context
context 'with nil company_type' do
# the subject of the spec
subject { profiles_helper.custom_company_type?(profile_with_company_type(nil)) }
# and english readability sweetness
it { should be_false }
end
context 'with blank company_type' do
subject { profiles_helper.custom_company_type?(profile_with_company_type('')) }
it { should be_false }
end
context 'with company_type == CustomCompanyType constant' do
subject do
profiles_helper.
custom_company_type?(profile_with_company_type(Profile::CustomCompanyType))
end
it { should be_true }
end
context 'with company_type == any other string' do
subject do
profiles_helper.custom_company_type?(profile_with_company_type("any string"))
end
it { should be_true }
end
end
end
for the sake os completeness, here is the helper you see used above
# spec/support/helpers.rb require 'ostruct' def profile_with_company_type(company_type) OpenStruct.new :company_type => company_type end
yes, really => lovely