spec/unit/models/message_spec.rb in ably-0.7.2 vs spec/unit/models/message_spec.rb in ably-0.7.4

- old
+ new

@@ -1,24 +1,25 @@ # encoding: utf-8 -require 'spec_helper' -require 'shared/model_behaviour' require 'base64' require 'msgpack' +require 'spec_helper' +require 'shared/model_behaviour' + describe Ably::Models::Message do include Ably::Modules::Conversions subject { Ably::Models::Message } let(:protocol_message_timestamp) { as_since_epoch(Time.now) } let(:protocol_message) { Ably::Models::ProtocolMessage.new(action: 1, timestamp: protocol_message_timestamp) } it_behaves_like 'a model', with_simple_attributes: %w(name client_id data encoding) do - let(:model_args) { [protocol_message] } + let(:model_args) { [protocol_message: protocol_message] } end context '#timestamp' do - let(:model) { subject.new({}, protocol_message) } + let(:model) { subject.new({}, protocol_message: protocol_message) } it 'retrieves attribute :timestamp as Time object from ProtocolMessage' do expect(model.timestamp).to be_a(Time) expect(model.timestamp.to_i).to be_within(1).of(Time.now.to_i) end @@ -37,11 +38,11 @@ expect(model.connection_id).to eql(model_connection_id) end end context 'with a protocol message with a different connectionId' do - let(:model) { subject.new({ 'connectionId' => model_connection_id }, protocol_message) } + let(:model) { subject.new({ 'connectionId' => model_connection_id }, protocol_message: protocol_message) } it 'uses the model value' do expect(model.connection_id).to eql(model_connection_id) end end @@ -55,21 +56,21 @@ expect(model.connection_id).to be_nil end end context 'with a protocol message with a connectionId' do - let(:model) { subject.new({ }, protocol_message) } + let(:model) { subject.new({ }, protocol_message: protocol_message) } it 'uses the model value' do expect(model.connection_id).to eql(protocol_connection_id) end end end end context 'Java naming', :api_private do - let(:model) { subject.new({ clientId: 'joe' }, protocol_message) } + let(:model) { subject.new({ clientId: 'joe' }, protocol_message: protocol_message) } it 'converts the attribute to ruby symbol naming convention' do expect(model.client_id).to eql('joe') end end @@ -78,11 +79,11 @@ %w(name client_id encoding).each do |attribute| context ":#{attribute}" do let(:encoded_value) { value.encode(encoding) } let(:value) { random_str } let(:options) { { attribute.to_sym => encoded_value } } - let(:model) { subject.new(options, protocol_message) } + let(:model) { subject.new(options, protocol_message: protocol_message) } let(:model_attribute) { model.public_send(attribute) } context 'as UTF_8 string' do let(:encoding) { Encoding::UTF_8 } @@ -140,28 +141,28 @@ context '#to_json', :api_private do let(:json_object) { JSON.parse(model.to_json) } context 'with valid data' do - let(:model) { subject.new({ name: 'test', clientId: 'joe' }, protocol_message) } + let(:model) { subject.new({ name: 'test', clientId: 'joe' }, protocol_message: protocol_message) } it 'converts the attribute back to Java mixedCase notation using string keys' do expect(json_object["clientId"]).to eql('joe') end end context 'with invalid data' do - let(:model) { subject.new({ clientId: 'joe' }, protocol_message) } + let(:model) { subject.new({ clientId: 'joe' }, protocol_message: protocol_message) } it 'raises an exception' do expect { model.to_json }.to raise_error RuntimeError, /cannot generate a valid Hash/ end end context 'with binary data' do let(:data) { MessagePack.pack(random_str(32)) } - let(:model) { subject.new({ name: 'test', data: data }, protocol_message) } + let(:model) { subject.new({ name: 'test', data: data }, protocol_message: protocol_message) } it 'encodes as Base64 so that it can be converted to UTF-8 automatically by JSON#dump' do expect(json_object["data"]).to eql(::Base64.encode64(data)) end @@ -186,11 +187,11 @@ id: protocol_message_id }) end context 'with protocol message' do - let(:model) { subject.new({ id: id, timestamp: message_timestamp }, protocol_message) } + let(:model) { subject.new({ id: id, timestamp: message_timestamp }, protocol_message: protocol_message) } specify '#id prefers embedded ID' do expect(model.id).to eql(id) end @@ -320,11 +321,11 @@ expect(subject.assigned_to_protocol_message?).to eql(false) end end context 'with ProtocolMessage' do - subject { Ably::Models.Message(json, protocol_message) } + subject { Ably::Models.Message(json, protocol_message: protocol_message) } it 'returns a Message object' do expect(subject).to be_a(Ably::Models::Message) end @@ -364,10 +365,10 @@ expect(subject.assigned_to_protocol_message?).to eql(false) end end context 'with ProtocolMessage' do - subject { Ably::Models.Message(message, protocol_message) } + subject { Ably::Models.Message(message, protocol_message: protocol_message) } it 'returns a Message object' do expect(subject).to be_a(Ably::Models::Message) end