Class: Riot::AssignsMacro

Inherits:
AssertionMacro show all
Defined in:
lib/riot/assertion_macros/assigns.rb

Overview

In the positive case, asserts that an instance variable is defined for the result of the assertion. Value of instance variable is expected to not be nil.

setup { User.new(:email => "[email protected]") }
asserts_topic.assigns(:email)

If a value is provided in addition to the variable name, the actual value of the instance variable must equal the expected value.

setup { User.new(:email => "[email protected]") }
asserts_topic.assigns(:email, "[email protected]")

In the negative case, asserts that an instance variables *is not* defined or has a nil value. If a value is provided in addition to the name, then ensure that the actual value does not equal the expected value.

setup { User.new(:email => "[email protected]") }
denies("topic") { topic }.assigns(:first_name)
denies("topic") { topic }.assigns(:email, "[email protected]")

Instance Attribute Summary

Attributes inherited from AssertionMacro

#file, #line

Instance Method Summary collapse

Methods inherited from AssertionMacro

#error, #expected_message, expects_exception!, #expects_exception?, #fail, #new_message, #pass, register, #should_have_message

Instance Method Details

#devaluate(actual, *expectings) ⇒ Array

Supports negative/converse assertion testing. This is also where magic happens.

Parameters:

  • actual (Object)

    the value returned from evaling the Assertion block

  • variable (Symbol, String)

    name of instance variable to look for

  • expected_value (Object, nil)

    an optional value to validate for the variable

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/riot/assertion_macros/assigns.rb', line 45

def devaluate(actual, *expectings)
  prepare(actual, *expectings) do |variable, expected_value, actual_value|
    if actual_value.nil? || (expected_value && expected_value != actual_value)
      if expected_value && actual_value
        pass new_message.assigns(variable).with(expected_value)
      else
        pass new_message.assigns(variable)
      end
    else
      message = expected_message(variable).to_not_be
      fail(expected_value.nil? ? message.assigned_a_value : message.assigned_with(expected_value))
    end
  end
end

#evaluate(actual, *expectings) ⇒ Array

Supports positive assertion testing. This is where magic happens.

Parameters:

  • actual (Object)

    the value returned from evaling the Assertion block

  • variable (Symbol, String)

    name of instance variable to look for

  • expected_value (Object, nil)

    an optional value to validate for the variable

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/riot/assertion_macros/assigns.rb', line 26

def evaluate(actual, *expectings)
  prepare(actual, *expectings) do |variable, expected_value, actual_value|
    if actual_value.nil?
      fail expected_message(variable).to_be_assigned_a_value
    elsif !expected_value.nil? && expected_value != actual_value
      fail expected_message(variable).to_be_assigned_with(expected_value).not(actual_value)
    else
      if expected_value && actual_value
        pass new_message.assigns(variable).with(expected_value)
      else
        pass new_message.assigns(variable)
      end
    end
  end
end