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 Method Summary collapse

Methods inherited from AssertionMacro

default, #error, expects_exception!, #expects_exception?, #fail, #pass, register

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 @#{variable} to be assigned a value")
  elsif !expected_value.nil? && expected_value != actual_value
    fail(%Q[expected @#{variable} to be equal to #{expected_value.inspect}, not #{actual_value.inspect}])
  else
    pass
  end
end