public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
add an :equals option to should_assign_to which checks that the assigned 
instance variable is equal to the eval of the string passed to the option
mjankowski (author)
Sat Sep 20 13:06:28 -0700 2008
tsaleh (committer)
Sat Sep 20 13:14:22 -0700 2008
commit  967b9fc67c8fbe5f96ecfb710b08993fed0f66da
tree    d3c9887beee0cc0aaeeee0f3403f478674f3ec3b
parent  b6069f736d4160068574461677e945585bc95a3d
...
101
102
103
 
 
104
105
106
107
108
 
109
110
111
112
113
 
 
114
115
116
 
 
 
 
 
 
 
 
 
 
117
118
119
...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
 
116
117
118
 
 
119
120
121
122
123
124
125
126
127
128
129
130
131
0
@@ -101,19 +101,31 @@ module ThoughtBot # :nodoc:
0
         #
0
         # Options:
0
         # * <tt>:class</tt> - The expected class of the instance variable being checked.
0
+ # * <tt>:equals</tt> - A string which is evaluated and compared for equality with
0
+ # the instance variable being checked.
0
         #
0
         # Example:
0
         #
0
         # should_assign_to :user, :posts
0
         # should_assign_to :user, :class => User
0
+ # should_assign_to :user, :equals => '@user'
0
         def should_assign_to(*names)
0
           opts = names.extract_options!
0
           names.each do |name|
0
             test_name = "assign @#{name}"
0
- test_name << " as #{opts[:class]}" if opts[:class]
0
+ test_name << " as class #{opts[:class]}" if opts[:class]
0
+ test_name << " which is equal to #{opts[:equals]}" if opts[:equals]
0
             should test_name do
0
- assert assigns(name.to_sym), "The action isn't assigning to @#{name}"
0
- assert_kind_of opts[:class], assigns(name.to_sym) if opts[:class]
0
+ assigned_value = assigns(name.to_sym)
0
+ assert assigned_value, "The action isn't assigning to @#{name}"
0
+ assert_kind_of opts[:class], assigned_value if opts[:class]
0
+ if opts[:equals]
0
+ instantiate_variables_from_assigns do
0
+ expected_value = eval(opts[:equals], self.send(:binding), __FILE__, __LINE__)
0
+ assert_equal expected_value, assigned_value,
0
+ "Instance variable @#{name} expected to be #{expected_value} but was #{assigned_value}"
0
+ end
0
+ end
0
             end
0
           end
0
         end
...
64
65
66
67
 
68
69
70
 
 
 
71
72
73
...
64
65
66
 
67
68
69
70
71
72
73
74
75
76
0
@@ -64,10 +64,13 @@ class PostsControllerTest < Test::Unit::TestCase
0
         get :index, :user_id => users(:first)
0
       end
0
       should_respond_with :success
0
- should_assign_to :user, :class => User
0
+ should_assign_to :user, :class => User, :equals => 'users(:first)'
0
       should_fail do
0
         should_assign_to :user, :class => Post
0
       end
0
+ should_fail do
0
+ should_assign_to :user, :equals => 'posts(:first)'
0
+ end
0
       should_assign_to :posts
0
       should_not_assign_to :foo, :bar
0
     end

Comments

    No one has commented yet.