spec/acfs/stub_spec.rb in acfs-0.42.0 vs spec/acfs/stub_spec.rb in acfs-0.43.0
- old
+ new
@@ -12,11 +12,11 @@
Acfs::Stub.allow_requests = false
end
describe '#called?' do
context 'without specified number' do
- let!(:operation) { Acfs::Stub.resource MyUser, :read, with: { id: 1 }, return: { id: 1, name: 'John Smith', age: 32 } }
+ let!(:operation) { Acfs::Stub.resource MyUser, :read, with: {id: 1}, return: {id: 1, name: 'John Smith', age: 32} }
it 'should allow to test if stub was called' do
MyUser.find 1
Acfs.run
@@ -36,12 +36,12 @@
end
describe '.resource' do
context 'with ambiguous stubs' do
before do
- Acfs::Stub.resource MyUser, :read, with: { id: 1 }, return: { id: 1, name: 'John Smith', age: 32 }
- Acfs::Stub.resource MyUser, :read, with: { id: 1 }, raise: :not_found
+ Acfs::Stub.resource MyUser, :read, with: {id: 1}, return: {id: 1, name: 'John Smith', age: 32}
+ Acfs::Stub.resource MyUser, :read, with: {id: 1}, raise: :not_found
end
it 'should raise error' do
MyUser.find 1
@@ -49,13 +49,13 @@
end
end
context 'with read action' do
before do
- Acfs::Stub.resource MyUser, :read, with: { id: 1 }, return: { id: 1, name: 'John Smith', age: 32 }
- Acfs::Stub.resource MyUser, :read, with: { id: 2 }, raise: SpecialCustomError
- Acfs::Stub.resource MyUser, :read, with: { id: 3 }, raise: :not_found
+ Acfs::Stub.resource MyUser, :read, with: {id: 1}, return: {id: 1, name: 'John Smith', age: 32}
+ Acfs::Stub.resource MyUser, :read, with: {id: 2}, raise: SpecialCustomError
+ Acfs::Stub.resource MyUser, :read, with: {id: 3}, raise: :not_found
end
it 'should allow to stub resource reads' do
user = MyUser.find 1
Acfs.run
@@ -79,12 +79,12 @@
end
end
context 'with type parameter' do
before do
- Acfs::Stub.resource Computer, :read, with: { id: 1 }, return: { id: 1, type: 'PC' }
- Acfs::Stub.resource Computer, :read, with: { id: 2 }, return: { id: 2, type: 'Mac' }
+ Acfs::Stub.resource Computer, :read, with: {id: 1}, return: {id: 1, type: 'PC'}
+ Acfs::Stub.resource Computer, :read, with: {id: 2}, return: {id: 2, type: 'Mac'}
end
it 'should create inherited type' do
pc = Computer.find 1
mac = Computer.find 2
@@ -97,32 +97,32 @@
end
end
context 'with create action' do
before do
- Acfs::Stub.resource Session, :create, with: { ident: 'john@exmaple.org', password: 's3cr3t' }, return: { id: 'longhash', user: 1 }
- Acfs::Stub.resource Session, :create, with: lambda { |op| op.data[:ident] == 'john@exmaple.org' && op.data[:password] == 'wrong' }, raise: 422
+ Acfs::Stub.resource Session, :create, with: {ident: 'john@exmaple.org', password: 's3cr3t'}, return: {id: 'longhash', user: 1}
+ Acfs::Stub.resource Session, :create, with: ->(op) { op.data[:ident] == 'john@exmaple.org' && op.data[:password] == 'wrong' }, raise: 422
end
it 'should allow stub resource creation' do
session = Session.create! ident: 'john@exmaple.org', password: 's3cr3t'
expect(session.id).to be == 'longhash'
expect(session.user).to be == 1
end
it 'should allow to raise error' do
- expect {
+ expect do
Session.create! ident: 'john@exmaple.org', password: 'wrong'
- }.to raise_error(::Acfs::InvalidResource)
+ end.to raise_error(::Acfs::InvalidResource)
end
end
context 'with list action' do
before do
Acfs::Stub.resource MyUser, :list,
- return: [{ id: 1, name: 'John Smith', age: 32 }, { id: 2, name: 'Anon', age: 12 }]
+ return: [{id: 1, name: 'John Smith', age: 32}, {id: 2, name: 'Anon', age: 12}]
end
it 'should return collection' do
users = MyUser.all
Acfs.run
@@ -156,12 +156,12 @@
end
context 'with header' do
before do
Acfs::Stub.resource Comment, :list,
- return: [{ id: 1, text: 'Foo' }, { id: 2, text: 'Bar' }],
- headers: headers
+ return: [{id: 1, text: 'Foo'}, {id: 2, text: 'Bar'}],
+ headers: headers
end
let!(:comments) { Comment.all }
let(:headers) { {'X-Total-Pages' => '2'} }
subject { Acfs.run; comments }
@@ -170,15 +170,21 @@
end
end
context 'with update action' do
before do
- Acfs::Stub.resource MyUser, :read, with: { id: 1 }, return: { id: 1, name: 'John Smith', age: 32 }
- Acfs::Stub.resource MyUser, :update, with: { id: 1, name: 'John Smith', age: 22 }, return: { id: 1, name: 'John Smith', age: 23 }
- Acfs::Stub.resource MyUser, :update, with: { id: 1, name: 'John Smith', age: 0 }, raise: 422
+ Acfs::Stub.resource MyUser, :read, with: {id: 1}, return: {id: 1, name: 'John Smith', age: 32}
+ Acfs::Stub.resource MyUser, :update, with: {id: 1, name: 'John Smith', age: 22}, return: {id: 1, name: 'John Smith', age: 23}
+ Acfs::Stub.resource MyUser, :update, with: {id: 1, name: 'John Smith', age: 0}, raise: 422
end
+ let!(:update_stub) do
+ Acfs::Stub.resource MyUser, :update,
+ with: {id: 1, name: 'Jane Smith'},
+ return: ->(op) { Hash[op.data.map {|k, v| [k, v.to_s.upcase] }] }
+ end
+
it 'should allow stub resource update' do
user = MyUser.find 1
Acfs.run
user.age = 22
@@ -192,33 +198,55 @@
Acfs.run
user.age = 0
user.save
- expect {
+ expect do
user.save!
- }.to raise_error(::Acfs::InvalidResource)
+ end.to raise_error(::Acfs::InvalidResource)
end
+
+ it 'should match partial :with' do
+ user = MyUser.find 1
+ Acfs.run
+
+ user.age = 5
+ user.name = 'Jane Smith'
+ user.save!
+
+ expect(update_stub).to be_called
+ end
+
+ it 'should process response body' do
+ user = MyUser.find 1
+ Acfs.run
+
+ user.age = 5
+ user.name = 'Jane Smith'
+ user.save!
+
+ expect(user.name).to eq 'JANE SMITH'
+ end
end
context 'with create action' do
before do
Acfs::Stub.resource MyUser, :create, with: {name: 'John Smith', age: 0}, raise: 422
end
it 'should allow to raise error' do
- expect {
- user = MyUser.create! name: 'John Smith', age: 0
- }.to raise_error(::Acfs::InvalidResource)
+ expect do
+ MyUser.create! name: 'John Smith', age: 0
+ end.to raise_error(::Acfs::InvalidResource)
end
end
end
describe '.allow_requests=' do
context 'when enabled' do
before do
Acfs::Stub.allow_requests = true
- stub_request(:get, 'http://users.example.org/users/2').to_return response({ id: 2, name: 'John', age: 26 })
+ stub_request(:get, 'http://users.example.org/users/2').to_return response(id: 2, name: 'John', age: 26)
end
it 'should allow real requests' do
@user = MyUser.find 2
expect { Acfs.run }.to_not raise_error