spec/response_spec.rb in saorin-0.1.4 vs spec/response_spec.rb in saorin-0.2.0
- old
+ new
@@ -7,28 +7,28 @@
:result => '123',
:error => nil,
:id => rand(1 << 31),
}
options = default_options.merge(options)
- Saorin::Response.new *options.values_at(:result, :error, :id, :version)
+ Saorin::Response.new options
end
describe '#initialize' do
context 'success' do
- before { @r = create_response :result => '123', :error => nil, :id => 123 }
+ before { @r = Saorin::Response.new :result => '123', :id => 123 }
subject { @r }
its(:version) { should eq Saorin::JSON_RPC_VERSION }
its(:result) { should eq '123' }
its(:error) { should be_nil }
its(:id) { should eq 123 }
its(:error?) { should be_false }
end
- context 'fail' do
+ context 'error' do
before do
@e = Saorin::InvalidRequest.new
- @r = create_response :result => nil, :error => @e, :id => 123
+ @r = Saorin::Response.new :error => @e, :id => 123
end
subject { @r }
its(:version) { should eq Saorin::JSON_RPC_VERSION }
its(:result) { should be_nil }
its(:error) { should eq @e }
@@ -42,25 +42,32 @@
create_response(:version => '1.0').should_not be_valid
create_response(:version => '2.0').should be_valid
create_response(:version => 2).should_not be_valid
end
- it 'result / error' do
- create_response(:error => nil, :result => 123).should_not be_valid
- create_response(:error => nil, :result => '123').should be_valid
- create_response(:error => nil, :result => []).should_not be_valid
- create_response(:error => nil, :result => {}).should_not be_valid
+ context 'result' do
+ def create_success_response(result)
+ create_response(:result => result, :error => nil)
+ end
- create_response(:result => nil, :error => 123).should_not be_valid
- create_response(:result => nil, :error => '123').should_not be_valid
- create_response(:result => nil, :error => nil).should_not be_valid
- create_response(:result => nil, :error => []).should_not be_valid
- create_response(:result => nil, :error => {}).should be_valid
- create_response(:result => nil, :error => Saorin::InvalidResponse.new).should be_valid
+ it('number') { create_success_response(123).should be_valid }
+ it('string') { create_success_response('123').should be_valid }
+ it('nil') { create_success_response(nil).should be_valid }
+ it('array') { create_success_response([]).should be_valid }
+ it('hash') { create_success_response({}).should be_valid }
+ end
- create_response(:error => nil, :result => nil).should_not be_valid
- create_response(:error => 123, :result => 123).should_not be_valid
+ context 'error' do
+ def create_fail_response(error)
+ create_response(:result => nil, :error => error)
+ end
+
+ it('number') { create_fail_response(123).should_not be_valid }
+ it('string') { create_fail_response('123').should_not be_valid }
+ it('array') { create_fail_response([]).should_not be_valid }
+ it('hash') { create_fail_response({}).should be_valid }
+ it('exception') { create_fail_response(Saorin::InvalidResponse.new).should be_valid }
end
it 'id' do
create_response(:id => 123).should be_valid
create_response(:id => '123').should be_valid
@@ -93,10 +100,10 @@
e = Saorin::InvalidRequest.new
h = create_response(:error => e).to_h
h.should_not include('result')
h.should include('error')
end
- it 'fail' do
+ it 'error' do
h = create_response(:error => nil).to_h
h.should include('result')
h.should_not include('error')
end
end