Module: Rucola::TestCase::InstanceMethods

Defined in:
lib/rucola/test_case.rb

Instance Method Summary collapse

Instance Method Details

#assigns(name, obj = nil) ⇒ Object

Lets you get an instance variable from the instance.

obj.instance_variable_set(:@some_attr, 'foo')
assigns(:some_attr) # => 'foo'

You can also set an instance variable in the instance.

obj.assigns(:some_attr, 'bar')
obj.instance_variable_get(:@some_attr) # => 'bar'


101
102
103
# File 'lib/rucola/test_case.rb', line 101

def assigns(name, obj = nil)
  obj.nil? ? instance_to_be_tested.instance_variable_get("@#{name}") : instance_to_be_tested.instance_variable_set("@#{name}", obj)
end

#class_to_be_testedObject

Returns the class that’s to be tested.



82
83
84
# File 'lib/rucola/test_case.rb', line 82

def class_to_be_tested
  self.class.instance_variable_get(:@class_to_be_tested)
end

#ib_outlet(name, obj) ⇒ Object

Defines instance variables in the instance which represent the ib_outlets. It basically just sets the instance variables, but also creates shorcut accessors to get at them from your tests.

def after_setup
  ib_outlet :textField, OSX::NSTextField.alloc.init
  p textField # => #<OSX::NSTextField:0xdfa3f4 class='NSTextField' id=0x1e842b0>
end

Note that not every class can be instantiated in a test. So you can also supply something like a mock.



115
116
117
118
119
120
121
122
123
# File 'lib/rucola/test_case.rb', line 115

def ib_outlet(name, obj)
  unless respond_to? name
    self.class.class_eval do
      define_method(name) { assigns(name) }
      private name
    end
  end
  assigns(name, obj)
end

#ib_outlets(outlets) ⇒ Object

Shortcut method to defined multiple ib_outlets by supplying a hash.

def after_setup
  ib_outlets :window => mock("Main Window"),
             :tableView => OSX::NSTableView.alloc.init,
             :searchField => OSX::NSSearchField.alloc.init
end


132
133
134
# File 'lib/rucola/test_case.rb', line 132

def ib_outlets(outlets)
  outlets.each {|k,v| ib_outlet(k, v) }
end

#instance_to_be_testedObject Also known as: controller

An instance of the class that’s to be tested.



87
88
89
# File 'lib/rucola/test_case.rb', line 87

def instance_to_be_tested
  @instance_to_be_tested ||= class_to_be_tested.alloc.init
end

#setupObject

Sets up the ib_outlets to all be stubs which respond to everything with nil.

In your test use #after_setup to do any custom setup.



63
64
65
66
67
68
# File 'lib/rucola/test_case.rb', line 63

def setup
  class_to_be_tested.defined_ib_outlets.each do |outlet|
    ib_outlet(outlet, stub_everything(outlet.to_s))
  end
  after_setup if respond_to? :after_setup
end

#teardownObject

Sets the ib_outlets and instance to be tested to nil at the end of the test.

In your test use #after_teardown to do any custom teardown.



73
74
75
76
77
78
79
# File 'lib/rucola/test_case.rb', line 73

def teardown
  class_to_be_tested.defined_ib_outlets.each do |outlet|
    instance_to_be_tested.instance_variable_set("@#{outlet}", nil)
  end
  @instance_to_be_tested = nil
  after_teardown if respond_to? :after_teardown
end