Leonard Kakande
Leonard Kakande

Reputation: 695

Passing a model instance to a helper method in rspec testing

I have a helper method in my app located in spec/support/utilities.rb I am trying to pass an instance of a model object to it but I haven't succeeded so the tests fail

here is the helper method

def attribute_not_present(model_instance,model_attrib)
  describe "when #{model_attrib} is not present" do
    before { model_instance.send("#{model_attrib}=", " ") }
      it { should_not be_valid }
    end
 end

in spec/model/tool_spec.rb i have this

require 'spec_helper'

describe Tool do
  before do
    @tool = FactoryGirl.create(:tool)    
  end

  @attribute_array = ["kind", "serial_number", "department", "size", 
  "description", "hours", "length"] 

  subject { @tool }  

  #checks for absence of any of the required attributes
  @attribute_array.each { |tool_attribute|
    attribute_not_present(@tool,tool_attribute)
  } 
end

the @tool seems not to be recognized in the helper

the sample failure is this

1) Tool when size is not present 
  Failure/Error: before { model_instance.send("#{model_attrib}=", " ") }
  NoMethodError:
    undefined method `size=' for nil:NilClass
  # ./spec/support/utilities.rb:3:in `block (2 levels) in attribute_not_present'

I am rails newbie

Upvotes: 0

Views: 318

Answers (2)

Frederick Cheung
Frederick Cheung

Reputation: 84114

At the point where attribute_not_present is called, @tool does not yet exist. Moreover, in one case self is the example group, where when the spec is actually run (and inside your before blocks) self is an instance of the example group.

You don't need to pass model_instance through at all though - you could instead just use subject i.e.

before { subject.send("#{model_attrib}=", " ") }

however.

You may also want to look at shared examples.

Upvotes: 1

Anthony
Anthony

Reputation: 15967

Ok - I think I understand what you're trying to do here. You're trying to do unit tests, specifically validations on your Tool class, is that right?

If so, I personally like to use the shoulda_matchers gem which I find to be very idiomatic with exactly this.

As an example, you could do:

describe Tool do 
  it { should validate_presence_of(:kind) }
  it { should validate_presnece_of(:serial_number) }
end

You can even do more with the validations, say you knew the :serial_number can only be an integer, you could do:

it { should validate_numericality_of(:serial_number).only_integer }

This is probably a better way to do unit level validations than a helper method as it's more Ruby-like.

Upvotes: 0

Related Questions