At
At
is a small library that allows you to access instance variables on an object as
if they were attr_accessor
s for testing purposes.
Basically, at
directly translates this:
value = object.instance_eval { @instance_variable }
object.instance_eval { @instance_variable = "#{value}!" }
into this:
value = object.at.instance_variable
object.at.instance_variable = "#{value}!"
Install
Bundler: gem 'at'
RubyGems: gem install at
Usage
If I want to test the output of the full_name
method in my User
class
below, I would normally have three options for testing all possible outcomes;
initialize a User
object for each test case, initialize one User
object and
use instance_eval
to set the instance variables individually, or create
attr_accessor
s for each instance variable I would like to test. In Rspec, I
can use assigns
to test the value of the instance variable, but I can't
get the value of the instance variable.
At
solves these problems.
require 'at'
class User
def initialize(first_name=nil, last_name=nil)
@first_name, @last_name = first_name, last_name
end
def full_name
[@first_name, @last_name].compact.join(" ")
end
end
describe User, '#full_name' do
it 'should output the full name correctly' do
subject.at.first_name = 'John'
subject.at.last_name = 'Doe'
subject.full_name.should == 'John Doe'
end
end
Check out the specs for a better usage example.
Contributing
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
- Fork the project
- Start a feature/bugfix branch
- Commit and push until you are happy with your contribution
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright
Copyright © 2012 Ryan Scott Lewis [email protected].
The MIT License (MIT) - See LICENSE for further details.