Skip to content

Commit

Permalink
RSpec3: no Database-Backend:..:Model-Specs are passing
Browse files Browse the repository at this point in the history
Updated model_helper to work with RSpec 3.
To_do: replace the old »should« expressions with the new »expect«-Syntax
  • Loading branch information
Hartmut Bischoff committed Dec 27, 2014
1 parent 28475fc commit c50477a
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--color
--format nested
--format documentation
1 change: 0 additions & 1 deletion .rvmrc

This file was deleted.

6 changes: 1 addition & 5 deletions spec/factories/ib_bag.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
FactoryGirl.define do
factory :empty_ib_bag, class:IB::Bag do
factory :empty_bag, class:IB::Bag do
symbol 'GOOG'
sec_type :bag
currency 'USD'
exchange 'SMART'
expire '201503'
end

factory :apple_bag, class:IB::Bag do
Expand Down Expand Up @@ -52,9 +51,6 @@
bag.combo_legs << build( :combo_leg, weight:weight, con_id:contract.con_id )

end
## puts "affterm :build"
# puts bag.inspect
# puts e.expire
end

end
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/ib_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

end
# liest einen Optionsdatensatz aus der TWS aus
factory :default_option, class:IB::Option do
symbol 'AAPL'
expiry '201301'
strike 600.5
right :put
end

factory :tws_option_contract, class:IB::Contract do
transient do
Expand Down
76 changes: 40 additions & 36 deletions spec/model_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'spec_helper'
require 'db_helper'

### this does now work in Rspec 3
[:props, :aliases, :errors, :assigns, :human, :associations, :collections].each do |aspect|
eval "def #{aspect}
(metadata[:#{aspect}] rescue example.metadata[:#{aspect}]) || {}
(metadata[:#{aspect}] rescue RSpec.current_example.metadata[:#{aspect}]) || {}
end"
end

Expand Down Expand Up @@ -120,10 +120,10 @@ def test_assigns cases, prop, name

was_valid = subject.valid?
expect { subject.send "#{prop}=", value }.to_not raise_error
subject.send("#{prop}").should == result
expect( subject.send("#{prop}")).to eq result
if was_valid
# Assignment keeps validity
subject.errors.messages.should_not have_key name
expect( subject.errors.messages).not_to have_key name
subject.should be_valid
end
end
Expand All @@ -133,7 +133,7 @@ def test_assigns cases, prop, name
it "#{prop} alias assignment changes #{name} property, and vice versa" do
# Assignment to alias changes property as well
subject.send "#{prop}=", value
subject.send("#{name}").should == result
expect( subject.send("#{name}")).to eq result

# Unsetting alias unsets property as well
subject.send "#{prop}=", nil # unset alias
Expand All @@ -142,15 +142,15 @@ def test_assigns cases, prop, name

# Assignment to original property changes alias as well
subject.send "#{name}=", value
subject.send("#{prop}").should == result
expect( subject.send("#{prop}")).to eq result
end
end
end
end
end
end

shared_examples_for 'Model with valid defaults' do
shared_examples 'Model with valid defaults' do
context 'instantiation without properties' do
subject { described_class.new }
let(:init_with_props?) { false }
Expand All @@ -166,21 +166,21 @@ def test_assigns cases, prop, name
end
end

shared_examples_for 'Model with invalid defaults' do
shared_examples 'Model with invalid defaults' do
context 'instantiation without properties' do
subject { described_class.new }

it_behaves_like 'Model instantiated empty'
end

context 'instantiation with properties' do
context 'instantiation with properties' do
subject { described_class.new props }

it_behaves_like 'Model instantiated with properties'
end
end

shared_examples_for 'Self-equal Model' do
shared_examples 'Self-equal Model' do
subject { described_class.new props }

it 'is self-equal ' do
Expand All @@ -192,7 +192,7 @@ def test_assigns cases, prop, name
end
end

shared_examples_for 'Model instantiated empty' do
shared_examples 'Model instantiated empty' do
let(:init_with_props?) { false }
it { should_not be_nil }

Expand All @@ -201,9 +201,9 @@ def test_assigns cases, prop, name
# p name, subject.send(name), value
case value
when Time
subject.send(name).should be_a Time
expect( subject.send(name)).to be_a Time
else
subject.send(name).should == value
expect( subject.send(name)).to eq value
end
end
end
Expand All @@ -212,7 +212,7 @@ def test_assigns cases, prop, name
it_behaves_like 'Invalid Model'
end

shared_examples_for 'Model instantiated with properties' do
shared_examples 'Model instantiated with properties' do
let(:init_with_props?) { true }

it 'auto-assigns all properties given to initializer' do
Expand All @@ -232,29 +232,33 @@ def test_assigns cases, prop, name
end
end

# subject :: SUBJECT: #<IB::Contract:0x00000002ecce60 @attributes={"created_at"=>2014-12-24 09:50:24 +0100, "updated_at"=>2014-12-24 09:50:24 +0100, "con_id"=>0, "right"=>"", "exchange"=>"SMART", "include_expired"=>false}>
it_behaves_like 'Model properties'
it_behaves_like 'Valid Model'
end

shared_examples_for 'Model properties' do
shared_examples 'Model properties' do

it 'leaves order_id alone, no aliasing' do
if subject.respond_to?(:order_id)
subject.order_id.should be_nil
if subject.respond_to?(:local_id=)
expect( subject.order_id).to be_nil
if subject.respond_to?(:local_id)
subject.local_id = 1313
subject.order_id.should be_nil
expect( subject.order_id).to be_nil
subject.order_id = 2222
subject.local_id.should == 1313
expect( subject.local_id).to eq 1313
end
end
end

it 'allows setting properties' do
expect {
props.each do |name, value|
puts "name: #{name}, value:#{value}"
unless name=='example'
subject.send("#{name}=", value)
subject.send(name).should == value
expect(subject.send(name)).to eq value
end
end
}.to_not raise_error
end
Expand All @@ -263,7 +267,7 @@ def test_assigns cases, prop, name
it "#{name} = #{value.inspect} #=> does not raise" do
expect {
subject.send("#{name}=", value)
subject.send(name).should == value
expect( subject.send(name)).to eq value
}.to_not raise_error
end
end
Expand All @@ -285,7 +289,7 @@ def test_assigns cases, prop, name

end

shared_examples_for 'Valid Model' do
shared_examples 'Valid Model' do

it 'validates' do
subject.should be_valid
Expand All @@ -295,41 +299,41 @@ def test_assigns cases, prop, name
it_behaves_like 'Valid DB-backed Model'
end

shared_examples_for 'Invalid Model' do
shared_examples 'Invalid Model' do

it 'does not validate' do
subject.should_not be_valid
subject.should be_invalid
subject.errors.should_not be_empty
subject.errors.messages.should == errors
expect( subject).not_to be_valid
expect( subject).to be_invalid
expect( subject.errors).not_to be_empty
expect( subject.errors.messages).to eq errors
end

it_behaves_like 'Invalid DB-backed Model'
end

shared_examples_for 'Contract' do
shared_examples 'Contract' do
it 'becomes invalid if assigned wrong :sec_type property' do
subject.sec_type = 'FOO'
subject.should be_invalid
subject.errors.messages[:sec_type].should include "should be valid security type"
expect( subject ).to be_invalid
expect(subject.errors.messages[:sec_type]).to include "should be valid security type"
end

it 'becomes invalid if assigned wrong :right property' do
subject.right = 'BAR'
subject.should be_invalid
subject.errors.messages[:right].should include "should be put, call or none"
expect( subject ).to be_invalid
expect( subject.errors.messages[:right]).to include "should be put, call or none"
end

it 'becomes invalid if assigned wrong :expiry property' do
subject.expiry = 'BAR'
subject.should be_invalid
subject.errors.messages[:expiry].should include "should be YYYYMM or YYYYMMDD"
expect( subject ).to be_invalid
expect( subject.errors.messages[:expiry]).to include "should be YYYYMM or YYYYMMDD"
end

it 'becomes invalid if primary_exchange is set to SMART' do
subject.primary_exchange = 'SMART'
subject.should be_invalid
subject.errors.messages[:primary_exchange].should include "should not be SMART"
expect( subject ).to be_invalid
expect( subject.errors.messages[:primary_exchange]).to include "should not be SMART"
end

end
24 changes: 12 additions & 12 deletions spec/models/ib/bag_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'model_helper'

## not tws-connection required
describe IB::Bag,

:props =>
Expand Down Expand Up @@ -46,25 +46,25 @@
it_behaves_like 'Model with valid defaults'
it_behaves_like 'Self-equal Model'

context 'properly initiated' do
subject { IB::Bag.new props }

context FactoryGirl.build( :butterfly ) do

it_behaves_like 'Contract'

it 'has extra legs_description accessor' do
subject.legs_description.should == "81032967|1,81032968|-2,81032973|1"
expect(subject.legs_description).to eq "258651|1,258652|-2,258653|1"
end
end

it 'correctly defines Contract type (sec_type) for Bag contract' do
[IB::Contract.new(:sec_type => :bag),
IB::Contract.new(:sec_type => 'BAG'),
IB::Bag.new
].each do |contract|
contract.should be_bag
contract.should_not be_bond
contract.should_not be_stock
contract.should_not be_option
[FactoryGirl.build( :ib_contract, :sec_type => :bag),
FactoryGirl.build( :ib_contract, :sec_type => 'BAG'),
FactoryGirl.build( :empty_bag ) ].each do |contract|
expect( contract).to be_bag
expect( contract).not_to be_bond
expect( contract).not_to be_stock
expect( contract).not_to be_option
expect( contract).to be_valid
end
end

Expand Down
Loading

0 comments on commit c50477a

Please sign in to comment.