Łukasz Kazimierz Bandzarewicz

@lucassus

Rspec Matcher for Testing Model's Attr_protected Fields

Custom matcher DSL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
RSpec::Matchers.define :have_something do |something|
  match do |actual|
    # let assume that object under the test has #has_something? method
    actual.has_something?(something)
  end

  failure_message_for_should do |actual|
    # generate and return the appropriate string.
  end

  failure_message_for_should_not do |actual|
    # generate and return the appropriate string.
  end

  description do
    # generate and return the appropriate string.
  end
end

describe Something do
  subject { Something.new }
  it { should have_something('a thing') }
end

Custom matcher for testing attr_protected

1
2
3
4
5
6
7
8
9
10
11
RSpec::Matchers.define :protect_attribute do |attribute, value = nil|
  match do |record|
    old_value = record[attribute]
    record.update_attributes(attribute.to_sym => value)
    record[attribute] == old_value
  end

  failure_message do
    "#{subject.class} should protect attribute #{attribute.inspect}"
  end
end
1
2
3
4
5
6
7
8
9
10
11
class Topic < ActiveRecord::Base
  attr_accessible :title, :message
end

describe Topic do
  it { should have_db_column(:title) }
  it { should have_db_column(:message) }
  it { should have_db_column(:status) }

  it { should protect_attribute(:status) }
end