No commit activity in last 3 years
No release in over 3 years
A matcher for testing object attributes.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 0.8
~> 2.13
~> 10.0
~> 3.0
~> 1.2
~> 0.30
~> 0.9
~> 1.0
 Project Readme

Gem Version Build Status Code Climate Coverage Status

Attribute Matcher

An RSpec matcher for validating attributes.

Installation

Add this line to your application's Gemfile:

gem 'attribute_matcher'

And then execute:

$ bundle

Or install it yourself as:

$ gem install attribute_matcher

Usage

With Ruby attr methods

require 'spec_helper'

class Person
  attr_accessor :name
  attr_reader   :age
  attr_writer   :status

  def initialize
    self.name = 'Joe'
  end

  protected

  attr_accessor :address

  private

  attr_accessor :ssn
end

describe Person do
  it { is_expected.to have_attribute(:name)   }
  it { is_expected.to have_attribute(:age)    }
  it { is_expected.to have_attribute(:status) }

  describe 'accessors' do
    it { is_expected.to have_attribute(:name).read_write   }
    it { is_expected.to have_attribute(:age).read_only     }
    it { is_expected.to have_attribute(:status).write_only }
  end

  describe 'visibility' do
    it { is_expected.to have_attribute(:address).with_reader(:protected) }
    it { is_expected.to have_attribute(:address).with_writer(:protected) }
    it { is_expected.to have_attribute(:ssn).with_reader(:private)       }
    it { is_expected.to have_attribute(:ssn).with_writer(:private)       }
  end

  describe 'values' do
    it { is_expected.to have_attribute(:name).with_value('Joe') }
    it { is_expected.to have_attribute(:age).with_value(nil)    }
  end

  describe 'type' do
    it { is_expected.to have_attribute(:name).of_type(String) }
  end
end

With Virtus

require 'spec_helper'
require 'virtus'

class VirtusPerson
  include Virtus.model

  attribute :name, String, default: 'Joe'
  attribute :age
  attribute :status

  attribute :ssn,     String, reader: :private,   writer: :private
  attribute :address, String, reader: :protected, writer: :protected
end

describe VirtusPerson do
  it { is_expected.to have_attribute(:name)   }
  it { is_expected.to have_attribute(:age)    }
  it { is_expected.to have_attribute(:status) }

  describe 'visibility' do
    it { is_expected.to have_attribute(:address).with_reader(:protected) }
    it { is_expected.to have_attribute(:address).with_writer(:protected) }
    it { is_expected.to have_attribute(:ssn).with_reader(:private)       }
    it { is_expected.to have_attribute(:ssn).with_writer(:private)       }
  end

  describe 'values' do
    it { is_expected.to have_attribute(:name).with_value('Joe') }
    it { is_expected.to have_attribute(:age).with_value(nil)    }
  end

  describe 'type' do
    it { is_expected.to have_attribute(:name).of_type(String) }
  end
end

Contributing

  1. Fork it ( https://github.com/dwhelan/attribute_matcher/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request