Skip to content

Instantly share code, notes, and snippets.

@CharlieHawker
Last active June 8, 2017 11:11
Show Gist options
  • Select an option

  • Save CharlieHawker/e466dc88c0b2875e02c8e9f04757dab8 to your computer and use it in GitHub Desktop.

Select an option

Save CharlieHawker/e466dc88c0b2875e02c8e9f04757dab8 to your computer and use it in GitHub Desktop.

Revisions

  1. CharlieHawker revised this gist Jun 8, 2017. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions direct_assigment_matchers.rb
    Original file line number Diff line number Diff line change
    @@ -5,16 +5,12 @@ module DirectAssignmentMatchers
    match do |object|
    !DirectAssignmentMatchers::public_assign(object, attribute)
    end

    diffable
    end

    RSpec::Matchers.define :allow_direct_assignment_of do |attribute|
    match do |object|
    !!DirectAssignmentMatchers::public_assign(object, attribute)
    end

    diffable
    end

    private
  2. CharlieHawker created this gist Jun 8, 2017.
    34 changes: 34 additions & 0 deletions direct_assigment_matchers.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    module DirectAssignmentMatchers
    require 'rspec/expectations'

    RSpec::Matchers.define :prevent_direct_assignment_of do |attribute|
    match do |object|
    !DirectAssignmentMatchers::public_assign(object, attribute)
    end

    diffable
    end

    RSpec::Matchers.define :allow_direct_assignment_of do |attribute|
    match do |object|
    !!DirectAssignmentMatchers::public_assign(object, attribute)
    end

    diffable
    end

    private

    def self.public_assign(object, attribute)
    begin
    object.public_send(:"#{attribute.to_s}=", 'test')
    true
    rescue NoMethodError
    false
    end
    end
    end

    RSpec.configure do |config|
    config.include DirectAssignmentMatchers
    end