Skip to content

Commit 7ea2b71

Browse files
refactor
1 parent 9a5b522 commit 7ea2b71

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

test/peripheral_test.dart

+19-33
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ void main() {
1919

2020
const PERIPHERAL_NAME = 'Peripheral name';
2121
const PERIPHERAL_ID = 'peripheral id';
22+
const SERVICE_UUID = 's1';
23+
const CHARACTERISTIC_UUID = 'c1';
24+
const TRANSACTION_ID = 't1';
25+
const DESCRIPTOR_UUID = "d1";
26+
2227

2328
ManagerForPeripheralMock managerForPeripheral;
2429
TransactionIdGeneratorMock transactionIdGeneratorMock;
@@ -128,14 +133,11 @@ void main() {
128133

129134
group("Discovery", () {
130135
test("use given transactionId", () async {
131-
//given
132-
const transactionId = "86791384-";
133-
134136
//when
135-
await peripheral.discoverAllServicesAndCharacteristics(transactionId: transactionId);
137+
await peripheral.discoverAllServicesAndCharacteristics(transactionId: TRANSACTION_ID);
136138

137139
//then
138-
verify(managerForPeripheral.discoverAllServicesAndCharacteristics(any, transactionId));
140+
verify(managerForPeripheral.discoverAllServicesAndCharacteristics(any, TRANSACTION_ID));
139141
});
140142

141143
test("use generated transactionId", () async {
@@ -171,11 +173,10 @@ void main() {
171173
test("should return characteristic for given service", () async {
172174
//given
173175
List<Characteristic> characteristics = [CharacteristicMock(), CharacteristicMock()];
174-
const serviceUuid = "123uuid";
175-
when(managerForPeripheral.characteristics(any, serviceUuid)).thenAnswer((_) => Future.value(characteristics));
176+
when(managerForPeripheral.characteristics(any, SERVICE_UUID)).thenAnswer((_) => Future.value(characteristics));
176177

177178
//when
178-
List<Characteristic> fetchedCharacteristic = await peripheral.characteristics(serviceUuid);
179+
List<Characteristic> fetchedCharacteristic = await peripheral.characteristics(SERVICE_UUID);
179180

180181
//then
181182
expect(fetchedCharacteristic, characteristics);
@@ -212,17 +213,14 @@ void main() {
212213
test("should write value to characteristic", () async {
213214
//given
214215
CharacteristicWithValue mockedCharacteristicWithValue = CharacteristicWithValueMock();
215-
const serviceUuid = 's1';
216-
const characteristicUuid = 'c1';
217-
const transactionId = 't1';
218216
Uint8List value = Uint8List.fromList([1, 4, 9]);
219217
const withResponse = false;
220218

221-
when(managerForPeripheral.writeCharacteristicForDevice(any, serviceUuid, characteristicUuid, value, withResponse, transactionId))
219+
when(managerForPeripheral.writeCharacteristicForDevice(any, SERVICE_UUID, CHARACTERISTIC_UUID, value, withResponse, TRANSACTION_ID))
222220
.thenAnswer((_) => Future.value(mockedCharacteristicWithValue));
223221

224222
//when
225-
CharacteristicWithValue characteristicWithValue = await peripheral.writeCharacteristic(serviceUuid, characteristicUuid, value, withResponse, transactionId: transactionId);
223+
CharacteristicWithValue characteristicWithValue = await peripheral.writeCharacteristic(SERVICE_UUID, CHARACTERISTIC_UUID, value, withResponse, transactionId: TRANSACTION_ID);
226224

227225
//then
228226
expect(characteristicWithValue, mockedCharacteristicWithValue);
@@ -249,14 +247,11 @@ void main() {
249247
CharacteristicWithValueMock(),
250248
CharacteristicWithValueMock()
251249
];
252-
const serviceUuid = 's1';
253-
const characteristicUuid = 'c1';
254-
const transactionId = 't1';
255-
when(managerForPeripheral.monitorCharacteristicForDevice(any, serviceUuid, characteristicUuid, transactionId))
250+
when(managerForPeripheral.monitorCharacteristicForDevice(any, SERVICE_UUID, CHARACTERISTIC_UUID, TRANSACTION_ID))
256251
.thenAnswer((_) => Stream.fromIterable(emittedValues));
257252

258253
//when
259-
Stream<CharacteristicWithValue> characteristicsStream = peripheral.monitorCharacteristic(serviceUuid, characteristicUuid, transactionId: transactionId);
254+
Stream<CharacteristicWithValue> characteristicsStream = peripheral.monitorCharacteristic(SERVICE_UUID, CHARACTERISTIC_UUID, transactionId: TRANSACTION_ID);
260255

261256
//then
262257
expect(characteristicsStream, emitsInOrder(emittedValues));
@@ -341,12 +336,10 @@ void main() {
341336
test("should return desriptors of characteristic", () async {
342337
//given
343338
List<Descriptor> descriptors = [DescriptorMock(), DescriptorMock()];
344-
const serviceUuid = "123uuid";
345-
const characteristicUuid = "c1";
346-
when(managerForPeripheral.descriptorsForPeripheral(any, serviceUuid, characteristicUuid)).thenAnswer((_) => Future.value(descriptors));
339+
when(managerForPeripheral.descriptorsForPeripheral(any, SERVICE_UUID, CHARACTERISTIC_UUID)).thenAnswer((_) => Future.value(descriptors));
347340

348341
//when
349-
List<Descriptor> fetchedDescriptors = await peripheral.descriptorsForCharacteristic(serviceUuid, characteristicUuid);
342+
List<Descriptor> fetchedDescriptors = await peripheral.descriptorsForCharacteristic(SERVICE_UUID, CHARACTERISTIC_UUID);
350343

351344
//then
352345
expect(fetchedDescriptors, descriptors);
@@ -355,15 +348,12 @@ void main() {
355348
test("should return Descriptor value", () async {
356349
//given
357350
DescriptorWithValue descriptorWithValue = DescriptorWithValueMock();
358-
const serviceUuid = "123uuid";
359-
const characteristicUuid = "c1";
360-
const descriptorUuid = "c1";
361351
const transactionId = "t1";
362-
when(managerForPeripheral.readDescriptorForPeripheral(any, serviceUuid, characteristicUuid, descriptorUuid, transactionId))
352+
when(managerForPeripheral.readDescriptorForPeripheral(any, SERVICE_UUID, CHARACTERISTIC_UUID, DESCRIPTOR_UUID, transactionId))
363353
.thenAnswer((_) => Future.value(descriptorWithValue));
364354

365355
//when
366-
DescriptorWithValueMock obtainedDescriptorWithValue = await peripheral.readDescriptor(serviceUuid, characteristicUuid, descriptorUuid, transactionId: transactionId);
356+
DescriptorWithValueMock obtainedDescriptorWithValue = await peripheral.readDescriptor(SERVICE_UUID, CHARACTERISTIC_UUID, DESCRIPTOR_UUID, transactionId: transactionId);
367357

368358
//then
369359
expect(obtainedDescriptorWithValue, descriptorWithValue);
@@ -386,17 +376,13 @@ void main() {
386376
test("should write value to descriptor", () async {
387377
//given
388378
DescriptorWithValue descriptorWithValue = DescriptorWithValueMock();
389-
const serviceUuid = "123uuid";
390-
const characteristicUuid = "c1";
391-
const descriptorUuid = "c1";
392-
const transactionId = "t1";
393379
Uint8List value = Uint8List.fromList([1, 4, 9]);
394380

395-
when(managerForPeripheral.writeDescriptorForPeripheral(any, serviceUuid, characteristicUuid, descriptorUuid, value, transactionId))
381+
when(managerForPeripheral.writeDescriptorForPeripheral(any, SERVICE_UUID, CHARACTERISTIC_UUID, DESCRIPTOR_UUID, value, TRANSACTION_ID))
396382
.thenAnswer((_) => Future.value(descriptorWithValue));
397383

398384
//when
399-
DescriptorWithValue obtainedDescriptorWithValue = await peripheral.writeDescriptor(serviceUuid, characteristicUuid, descriptorUuid, value, transactionId: transactionId);
385+
DescriptorWithValue obtainedDescriptorWithValue = await peripheral.writeDescriptor(SERVICE_UUID, CHARACTERISTIC_UUID, DESCRIPTOR_UUID, value, transactionId: TRANSACTION_ID);
400386

401387
//then
402388
expect(obtainedDescriptorWithValue, descriptorWithValue);

0 commit comments

Comments
 (0)