Class: Riot::AssignsMacro

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

Overview

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]") }
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]") }
topic.assigns(:email, "[email protected]")

Instance Attribute Summary

Attributes inherited from AssertionMacro

#file, #line

Instance Method Summary collapse

Methods inherited from AssertionMacro

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

Instance Method Details

#evaluate(actual, *expectings) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/riot/assertion_macros/assigns.rb', line 14

def evaluate(actual, *expectings)
  variable, expected_value = expectings
  variable_name = "@#{variable}"
  actual_value = actual.instance_variable_defined?(variable_name) ? actual.instance_variable_get(variable_name) : nil
  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_equal_to(expected_value).not(actual_value)
  else
    pass
  end
end