Skip to content
This repository was archived by the owner on Sep 30, 2018. It is now read-only.

Commit 941e643

Browse files
committedNov 13, 2013
Use rspec for running specs
1 parent 1fc9329 commit 941e643

14 files changed

+103
-114
lines changed
 

‎Gemfile

-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
source 'https://rubygems.org'
22
gemspec
3-
4-
gem 'opal', :github => 'opal/opal'
5-
gem 'opal-jquery', :github => 'opal/opal-jquery'
6-
gem 'opal-spec', :github => 'opal/opal-spec'

‎Rakefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ require 'bundler'
22
Bundler.require
33
require 'opal/activesupport'
44

5-
require 'opal/spec/rake_task'
6-
Opal::Spec::RakeTask.new(:default)
5+
require 'opal/rspec/rake_task'
6+
Opal::RSpec::RakeTask.new(:default)

‎opal/vienna/record_array.rb

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def initialize
1111
@records = []
1212
end
1313

14+
def ==(arr)
15+
@records == arr
16+
end
17+
1418
def method_missing(sym, *args, &block)
1519
@records.__send__(sym, *args, &block)
1620
end

‎spec/eventable_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def events
3838

3939
obj.off(:foo, handler)
4040
obj.trigger(:foo)
41-
called.should be_false
41+
called.should eq(false)
4242
end
4343
end
4444

‎spec/model/accessing_attributes_spec.rb

-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ def last_name=(name)
1818
model.first_name = "Adam"
1919
model[:first_name].should eq("Adam")
2020
end
21-
22-
it "raises an exception when accessing an undefined attribute" do
23-
lambda { model[:foo] }.should raise_error(Exception)
24-
end
2521
end
2622

2723
describe "#[]=" do

‎spec/model/attribute_spec.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ class ModelAttributeSpec < Vienna::Model
88
describe ".attribute" do
99

1010
let(:model) { ModelAttributeSpec.new }
11-
let(:attributes) { model.instance_variable_get(:@attributes) }
1211

1312
it "should create a reader method for attribute" do
14-
model.respond_to?(:first_name).should be_true
13+
model.should respond_to(:first_name)
1514
end
1615

1716
it "should create a writer method for attribute" do
18-
model.respond_to?(:first_name=).should be_true
17+
model.should respond_to(:first_name=)
1918
end
2019

2120
describe "writer" do

‎spec/model/initialize_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ class ModelInitializeSpec < Vienna::Model
3636
end
3737

3838
it "marks the model as being a new record" do
39-
ModelInitializeSpec.new.new_record?.should be_true
39+
ModelInitializeSpec.new.should be_new_record
4040
end
4141

4242
it "marks the model as not being loaded" do
43-
ModelInitializeSpec.new.loaded?.should be_false
43+
ModelInitializeSpec.new.should_not be_loaded
4444
end
4545
end
4646
end

‎spec/model/load_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
it "marks the model instance as loaded" do
99
model.load({})
10-
model.loaded?.should be_true
10+
model.should be_loaded
1111
end
1212

1313
it "marks the model as not being a new record" do
1414
model.load({})
15-
model.new_record?.should be_false
15+
model.should_not be_new_record
1616
end
1717
end
1818
end

‎spec/model/persistence_spec.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@
1111
called = false
1212
model.on(:destroy) { called = true }
1313
model.did_destroy
14-
called.should be_true
14+
called.should eq(true)
1515
end
1616

1717
it "triggers a :destroy event on the class" do
1818
called = false
1919
SimpleModel.on(:destroy) { called = true }
2020
model.did_destroy
21-
called.should be_true
21+
called.should eq(true)
2222
end
2323

2424
it "removes the record from the class identity_map" do
2525
model = SimpleModel.load(:first_name => "Adam", id: 872)
2626

2727
model.did_destroy
28-
SimpleModel.identity_map[model.id].should be_nil
28+
SimpleModel.identity_map[model.id].should eq(nil)
2929
end
3030
end
3131

3232
describe "#did_create" do
3333
it "sets @new_record to false" do
3434
model.did_create
35-
model.new_record?.should be_false
35+
model.new_record?.should eq(false)
3636
end
3737

3838
it "adds record to class identity_map" do
@@ -51,14 +51,14 @@
5151
called = false
5252
model.on(:create) { called = true }
5353
model.did_create
54-
called.should be_true
54+
called.should eq(true)
5555
end
5656

5757
it "triggers a :create event on the class" do
5858
called = false
5959
model.class.on(:create) { called = true }
6060
model.did_create
61-
called.should be_true
61+
called.should eq(true)
6262
end
6363
end
6464

‎spec/model_spec.rb

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
require 'spec_helper'
22

33
describe Vienna::Model do
4-
before do
5-
SimpleModel.reset!
6-
AdvancedModel.reset!
7-
end
8-
94
describe ".new" do
105
it "should set @new_record to true" do
11-
SimpleModel.new.new_record?.should be_true
6+
SimpleModel.new.new_record?.should eq(true)
127
end
138
end
149

@@ -35,7 +30,7 @@
3530

3631
it "should set @new_record to false on the model" do
3732
model = SimpleModel.load(id: 42)
38-
model.new_record?.should be_false
33+
model.new_record?.should eq(false)
3934
end
4035

4136
it "should cache model" do

‎spec/route_spec.rb

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,60 @@
11
require 'spec_helper'
22

33
describe Vienna::Router::Route do
4-
describe "#initialize" do
5-
let(:route) { Vienna::Router::Route }
4+
subject { described_class }
65

6+
describe "#initialize" do
77
it "creates a regexp from the given pattern" do
8-
route.new('foo').regexp.should eq(/^foo$/)
8+
subject.new('foo').regexp.should eq(/^foo$/)
99
end
1010

1111
it "escapes slashes in the pattern" do
12-
route.new('/foo/bar/').regexp.should eq(/^\/foo\/bar\/$/)
12+
subject.new('/foo/bar/').regexp.should eq(/^\/foo\/bar\/$/)
1313
end
1414

1515
it "finds named params in pattern" do
16-
r = route.new('/foo/:bar')
16+
r = subject.new('/foo/:bar')
1717
r.named.should eq(['bar'])
1818

19-
p = route.new('/:woosh/:kapow')
19+
p = subject.new('/:woosh/:kapow')
2020
p.named.should eq(['woosh', 'kapow'])
2121
end
2222

2323
it "finds splatted params in pattern" do
24-
route.new('/foo/*baz').named.should eq(['baz'])
24+
subject.new('/foo/*baz').named.should eq(['baz'])
2525
end
2626

2727
it "produces a regexp to match given pattern" do
28-
route.new('/foo').regexp.match('/bar').should be_nil
29-
route.new('/foo').regexp.match('/foo').should be_kind_of(MatchData)
28+
subject.new('/foo').regexp.match('/bar').should be_nil
29+
subject.new('/foo').regexp.match('/foo').should be_kind_of(MatchData)
3030
end
3131
end
3232

3333
describe "#match" do
34-
let(:route) { Vienna::Router::Route }
35-
3634
it "returns false for a non matching route" do
37-
route.new('/foo').match('/a/b/c').should be_false
35+
subject.new('/foo').match('/a/b/c').should eq(false)
3836
end
3937

4038
it "returns true for a matching route" do
41-
route.new('/foo').match('/foo').should be_true
39+
subject.new('/foo').match('/foo').should eq(true)
4240
end
4341

4442
it "calls block given to #initialize on matching a route" do
4543
called = false
46-
route.new('/foo') { called = true }.match('/foo')
47-
called.should be_true
44+
subject.new('/foo') { called = true }.match('/foo')
45+
called.should eq(true)
4846
end
4947

5048
it "calls handler with an empty hash for a simple route" do
51-
route.new('/foo') { |params| params.should eq({}) }.match('/foo')
49+
subject.new('/foo') { |params| params.should eq({}) }.match('/foo')
5250
end
5351

5452
it "returns a hash of named params for matching route" do
55-
route.new('/foo/:first') { |params|
53+
subject.new('/foo/:first') { |params|
5654
params.should eq({'first' => '123' })
5755
}.match('/foo/123')
5856

59-
route.new('/:first/:second') { |params|
57+
subject.new('/:first/:second') { |params|
6058
params.should eq({ 'first' => 'woosh', 'second' => 'kapow' })
6159
}.match('/woosh/kapow')
6260
end

‎spec/router_spec.rb

+32-34
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,102 @@
11
require 'spec_helper'
22

33
describe Vienna::Router do
4-
let(:router) { Vienna::Router.new }
5-
64
describe "#update" do
75
it "should update Router.path" do
86
$global.location.hash = "#/hello/world"
9-
router.update
7+
subject.update
108

11-
router.path.should eq('/hello/world')
9+
subject.path.should eq('/hello/world')
1210
end
1311

1412
it "calls #match with the new @path" do
1513
$global.location.hash = "#/foo/bar"
1614
called = nil
1715

18-
router.define_singleton_method(:match) { |m| called = m }
16+
subject.define_singleton_method(:match) { |m| called = m }
1917
called.should be_nil
2018

21-
router.update
19+
subject.update
2220
called.should eq("/foo/bar")
2321
end
2422
end
2523

2624
describe "#route" do
2725
it "should add a route" do
28-
router.route('/users') {}
29-
router.routes.size.should eq(1)
26+
subject.route('/users') {}
27+
subject.routes.size.should eq(1)
3028

31-
router.route('/hosts') {}
32-
router.routes.size.should eq(2)
29+
subject.route('/hosts') {}
30+
subject.routes.size.should eq(2)
3331
end
3432
end
3533

3634
describe "#match" do
3735
it "returns nil when no routes on router" do
38-
router.match('/foo').should be_nil
36+
subject.match('/foo').should be_nil
3937
end
4038

4139
it "returns a matching route for the path" do
42-
a = router.route('/foo') {}
43-
b = router.route('/bar') {}
40+
a = subject.route('/foo') {}
41+
b = subject.route('/bar') {}
4442

45-
router.match('/foo').should eq(a)
46-
router.match('/bar').should eq(b)
43+
subject.match('/foo').should eq(a)
44+
subject.match('/bar').should eq(b)
4745
end
4846

4947
it "returns nil when there are no matching routes" do
50-
router.route('/woosh') {}
51-
router.route('/kapow') {}
48+
subject.route('/woosh') {}
49+
subject.route('/kapow') {}
5250

53-
router.match('/ping').should be_nil
51+
subject.match('/ping').should be_nil
5452
end
5553

5654
it "calls handler of matching route" do
5755
out = []
58-
router.route('/foo') { out << :foo }
59-
router.route('/bar') { out << :bar }
56+
subject.route('/foo') { out << :foo }
57+
subject.route('/bar') { out << :bar }
6058

61-
router.match('/foo')
59+
subject.match('/foo')
6260
out.should eq([:foo])
6361

64-
router.match('/bar')
62+
subject.match('/bar')
6563
out.should eq([:foo, :bar])
6664

67-
router.match('/eek')
65+
subject.match('/eek')
6866
out.should eq([:foo, :bar])
6967
end
7068

7169
it "works with / too" do
7270
out = []
73-
router.route('/') { out << :index }
71+
subject.route('/') { out << :index }
7472

7573
$global.location.hash = ""
76-
router.update
74+
subject.update
7775

7876
$global.location.hash = "#/"
79-
router.update
77+
subject.update
8078

8179
out.should == [:index, :index]
8280
end
8381
end
8482

8583
describe "#navigate" do
8684
it "should update location.hash" do
87-
router.navigate "foo"
85+
subject.navigate "foo"
8886
$global.location.hash.should eq("#foo")
8987
end
9088

9189
it "triggers the route matchers" do
9290
called = false
93-
router.route("/foo") { called = true }
91+
subject.route("/foo") { called = true }
9492

95-
router.navigate("/bar")
96-
router.update
97-
called.should be_false
93+
subject.navigate("/bar")
94+
subject.update
95+
called.should eq(false)
9896

99-
router.navigate("/foo")
100-
router.update
101-
called.should be_true
97+
subject.navigate("/foo")
98+
subject.update
99+
called.should eq(true)
102100
end
103101
end
104102
end

‎spec/spec_helper.rb

+30-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require 'vendor/jquery'
2-
require 'opal-spec'
2+
require 'opal-rspec'
33
require 'opal-jquery'
44
require 'vienna'
55
require 'vienna/template_view'
@@ -12,35 +12,38 @@ class AdvancedModel < Vienna::Model
1212
primary_key :title
1313
end
1414

15-
##
16-
# Spec helpers
15+
module ViennaSpecHelper
16+
# Add some html to the document for tests to use. Code is added then
17+
# removed before each example is run.
18+
#
19+
# describe "some feature" do
20+
# html "<div>bar</div>"
21+
#
22+
# it "should have bar content" do
23+
# # ...
24+
# end
25+
# end
26+
#
27+
# @param [String] html_string html content
28+
def html(html_string = "")
29+
html = %Q{<div id="vienna-spec-test-div">#{html_string}</div>}
1730

18-
module OpalSpec
19-
class Example
31+
before do
32+
@_spec_html = Element.parse(html)
33+
@_spec_html.append_to_body
34+
end
2035

21-
# Add some html to the document for tests to use. Code is added then
22-
# removed before each example is run.
23-
#
24-
# describe "some feature" do
25-
# html "<div>bar</div>"
26-
#
27-
# it "should have bar content" do
28-
# # ...
29-
# end
30-
# end
31-
#
32-
# @param [String] html_string html content
33-
def self.html(html_string = "")
34-
html = %Q{<div id="vienna-spec-test-div">#{html_string}</div>}
36+
after do
37+
@_spec_html.remove
38+
end
39+
end
40+
end
3541

36-
before do
37-
@_spec_html = Element.parse(html)
38-
@_spec_html.append_to_body
39-
end
42+
RSpec.configure do |config|
43+
config.extend ViennaSpecHelper
4044

41-
after do
42-
@_spec_html.remove
43-
end
44-
end
45+
config.before(:each) do
46+
SimpleModel.reset!
47+
AdvancedModel.reset!
4548
end
4649
end

‎vienna.gemspec

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ Gem::Specification.new do |s|
1616
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
1717
s.require_paths = ['lib']
1818

19-
s.add_dependency 'opal', '>= 0.4.3'
20-
s.add_dependency 'opal-jquery', '>= 0.0.12'
21-
s.add_dependency 'opal-activesupport', '>= 0.0.4'
19+
s.add_dependency 'opal', '~> 0.5.0'
20+
s.add_dependency 'opal-jquery'
21+
s.add_dependency 'opal-activesupport'
2222

23-
s.add_development_dependency 'opal-spec'
23+
s.add_development_dependency 'opal-rspec', '~> 0.2.0'
2424
s.add_development_dependency 'rake'
2525
end

0 commit comments

Comments
 (0)
This repository has been archived.