File: README.rdoc

package info (click to toggle)
ruby-jnunemaker-matchy 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 272 kB
  • ctags: 555
  • sloc: ruby: 2,939; makefile: 2
file content (162 lines) | stat: -rw-r--r-- 4,767 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
= matchy

* http://github.com/jeremymcanally/matchy

== DESCRIPTION:
Hate writing assertions? Need a little behavior-driven love in your tests? Then matchy is for you.

== FEATURES/PROBLEMS:

* Get the beauty of RSpec without all the overhead
* Create your own matchers with ease

== SYNOPSIS:

* Get BDD on your objects

    x = 13 * 4
    x.should == 42
    
    y = "hello"
    y.length.should_not be(4)

* Use familiar syntax to specify things

    # RSpec
    "my string".should =~ /string/
    lambda { raise "FAIL" }.should raise_error
    
    # matchy
    "my string".should =~ /string/
    lambda { raise "FAIL" }.should raise_error

* Most of familiar RSpec Matchers are built in

    # raise_error matcher
    lambda {raise}.should raise_error                                  #pass
    lambda {raise MyCustomError.new}.should raise_error(MyCustomError) #pass
    lambda {raise "message"}.should raise_error("message")             #pass
    lambda {raise "message"}.should raise_error(/essa/)                #pass

    # change matcher
    lambda {@var+=1}.should change {@var}
    # passes
    lambda { }.should change {@var}
    # fails
    @var = 1
    lambda {@var+=1}.should change {@var}.from(1).to(2)
    # passes

    # be_something matcher
    @obj.should be_something
    # passes if @obj.something? is true

    * a lot more ...

* Create your own custom matchers
    
    # maybe in your test helper
    class Test::Unit::TestCase
      custom_matcher :be_nil do |receiver, matcher, args|
        receiver.nil?
      end
      
      # also you can set positive (should) and negative (should not) failure messages
      custom_matcher :be_nil do |receiver, matcher, args|
        matcher.positive_failure_message = "Expected #{receiver} to be nil but it wasn't"
        matcher.negative_failure_message = "Expected #{receiver} not to be nil but it was"
        receiver.nil?
      end
    end
    
    # your actual test
    class NilTest < Test::Unit::TestCase
      def test_nil_stuff
        nil.should be_nil       # pass
        nil.should_not be_nil   # fail
        'foo'.should_not be_nil # pass
        'foo'.should be_nil     # fail
      end
    end
    
    # Matchers can accept arguments
    class Test::Unit::TestCase
      custom_matcher :have_error_on do |receiver, matcher, args|
        attribute = args[0]
        
        receiver.valid?
        receiver.errors.on(attribute).should_not == nil
      end
    end
    
    class ArgumentTest < Test::Unit::TestCase
      class Item < ActiveRecord::Base
        validate_presence_of :title
      end
      
      def test_arguments
        item = Item.new
        item.should have_error_on(:title)     # pass
        item.title = 'Foo'
        item.should_not have_error_on(:title) # pass
      end
    end
    
    # Even more advanced, you can have messages on matchers
    class Test::Unit::TestCase
      custom_matcher :have do |receiver, matcher, args|  
        count = args[0]
        something = matcher.chained_messages[0].name
        actual = receiver.send(something).size
        matcher.positive_failure_message = "Expected #{receiver} to have #{actual} #{something}, but found #{count} "
        actual == count
      end
    end
    
    class MoreAdvancedTest < Test::Unit::TestCase
      class Item
        def tags
          %w(foo bar baz)
        end
      end
      
      def test_item_has_tags
        item = Item.new
        item.should have(3).tags # pass
        item.should have(2).tags # fail
      end
    end

== REQUIREMENTS:

* Test::Unit (you got it)

== INSTALL:

    $ gem sources -a http://gems.github.com
    $ sudo gem install jnunemaker-matchy

== LICENSE:

(The MIT License)

Copyright (c) 2008 Jeremy McAnally

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.