File: README.md

package info (click to toggle)
ruby-em-spec 0.2.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 172 kB
  • sloc: ruby: 500; makefile: 3
file content (147 lines) | stat: -rw-r--r-- 3,092 bytes parent folder | download | duplicates (3)
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
# Simple BDD API for testing asynchronous Ruby/EventMachine code

`em-spec` can be used with either bacon, test unit or rspec.

## Rspec
There are two ways to use with the Rspec gem.

To use it as a helper, include `EM::SpecHelper` in your describe block.
You then use the `em` method to wrap your evented test code.

Inside the `em` block, you must call `done` after your expectations.
Everything works normally otherwise.

```ruby
  require "em-spec/rspec"
  describe EventMachine do
    include EM::SpecHelper
  
    it "works normally when not using #em" do
      1.should == 1
    end
  
    it "makes testing evented code easy with #em" do
      em do
        start = Time.now

        EM.add_timer(0.5){
          (Time.now-start).should be_close( 0.5, 0.1 )
          done
        }
      end
    end
  end
```

The other option is to include `EM::Spec` in your describe block.

This will patch Rspec so that all of your examples run inside an `em` block automatically:

```ruby
  require "em-spec/rspec"
  describe EventMachine do
    include EM::Spec
    
    it "requires a call to #done every time" do
      1.should == 1
      done
    end
    
    it "runs test code in an em block automatically" do
      start = Time.now

      EM.add_timer(0.5){
        (Time.now-start).should be_close( 0.5, 0.1 )
        done
      }
    end
  end
```

## Bacon

The API is identical to Bacon, except that you must explicitly call `done`
after all the current behavior's assertions have been made:

```ruby
  require 'em-spec/bacon'

  EM.describe EventMachine do

    should 'have timers' do
      start = Time.now

      EM.add_timer(0.5){
        (Time.now-start).should.be.close 0.5, 0.1
        done
      }
    end

    should 'have periodic timers' do
      num = 0
      start = Time.now

      timer = EM.add_periodic_timer(0.5){
        if (num += 1) == 2
          (Time.now-start).should.be.close 1.0, 0.1
          EM.__send__ :cancel_timer, timer
          done
        end
      }
    end

  end
```

## `Test::Unit`

There are two ways to use the `Test::Unit` extension.

To use it as a helper, include `EM::TestHelper` in your test unit class.

You then use the em method to wrap your evented test code.

Inside the em block, you must call `done` after your expectations.
Everything works normally otherwise.

```ruby
  require 'em-spec/test'

  class EmSpecHelperTest < Test::Unit::TestCase
    include EventMachine::TestHelper
  
    def test_trivial
      em do
        assert_equal 1, 1
        done
      end
    end
  end
```

The other option is to include `EM::Test` in your test class.

This will patch `Test::Unit` so that all of your examples run inside an
`em` block automatically:

```ruby
  require 'em-spec/test'

  class EmSpecTest < Test::Unit::TestCase
  
    include EventMachine::Test

    def test_timer
      start = Time.now

      EM.add_timer(0.5){
        assert_in_delta 0.5, Time.now-start, 0.1
        done
      }
    end
  end
```

## Resources

* [Bacon](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/30b07b651b0662fd)