Skip to content

Commit 6c3a6ce

Browse files
committed
Replacing self written OTAStorage_Mock with fakeit mocking framework
1 parent 80b651e commit 6c3a6ce

File tree

4 files changed

+54
-115
lines changed

4 files changed

+54
-115
lines changed

extras/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ project(testArduinoIoTCloud)
1111
include_directories(include)
1212
include_directories(../../src/utility/ota)
1313
include_directories(external/catch/v2.12.1/include)
14+
include_directories(external/fakeit/v2.0.5/include)
1415

1516
##########################################################################
1617

@@ -25,7 +26,6 @@ set(TEST_TARGET testArduinoIoTCloud)
2526
set(TEST_SRCS
2627
src/test_main.cpp
2728
src/test_OTALogic.cpp
28-
src/OTAStorage_Mock.cpp
2929
src/OTATestData.cpp
3030
../../src/utility/ota/crc.cpp
3131
../../src/utility/ota/OTALogic.cpp

extras/test/include/OTAStorage_Mock.h

Lines changed: 0 additions & 42 deletions
This file was deleted.

extras/test/src/OTAStorage_Mock.cpp

Lines changed: 0 additions & 56 deletions
This file was deleted.

extras/test/src/test_OTALogic.cpp

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@
66
INCLUDE
77
**************************************************************************************/
88

9+
#include <vector>
910
#include <algorithm>
1011

1112
#include <catch.hpp>
13+
#include <fakeit.hpp>
1214

1315
#include <OTATestData.h>
14-
#include <OTAStorage_Mock.h>
1516

1617
#include <OTALogic.h>
18+
#include <OTAStorage.h>
19+
20+
/**************************************************************************************
21+
NAMESPACE
22+
**************************************************************************************/
23+
24+
using namespace fakeit;
1725

1826
/**************************************************************************************
1927
TEST HELPER
@@ -43,22 +51,39 @@ void simulateOTABinaryReception(OTALogic & ota_logic, OTAData const & ota_test_d
4351

4452
TEST_CASE("Valid OTA data is received ", "[OTALogic]")
4553
{
46-
OTAStorage_Mock ota_storage;
47-
OTALogic ota_logic(ota_storage);
54+
Mock<OTAStorage> ota_storage;
55+
std::vector<uint8_t> ota_binary_data;
56+
57+
/* Configure mock object */
58+
When(Method(ota_storage, init)).Return(true);
59+
When(Method(ota_storage, open)).Return(true);
60+
When(Method(ota_storage, write)).AlwaysDo(
61+
[&ota_binary_data](uint8_t const * const buf, size_t const num_bytes) -> size_t
62+
{
63+
std::copy(buf, buf + num_bytes, std::back_inserter(ota_binary_data));
64+
return num_bytes;
65+
});
66+
Fake(Method(ota_storage, close));
67+
Fake(Method(ota_storage, remove));
68+
Fake(Method(ota_storage, deinit));
69+
70+
71+
/* Generate test data */
4872
OTAData valid_ota_test_data;
49-
50-
ota_storage._init_return_val = true;
51-
ota_storage._open_return_val = true;
52-
5373
generate_valid_ota_data(valid_ota_test_data);
5474

75+
76+
/* Perform test */
77+
OTALogic ota_logic(ota_storage.get());
5578
simulateOTABinaryReception(ota_logic, valid_ota_test_data);
5679

80+
81+
/* Perform checks */
5782
THEN("the complete binary file should have been written to the OTA storage")
5883
{
59-
REQUIRE(ota_storage._binary.size() == valid_ota_test_data.data.len);
60-
REQUIRE(std::equal(ota_storage._binary.begin(),
61-
ota_storage._binary.end(),
84+
REQUIRE(ota_binary_data.size() == valid_ota_test_data.data.len);
85+
REQUIRE(std::equal(ota_binary_data.begin(),
86+
ota_binary_data.end(),
6287
valid_ota_test_data.data.bin));
6388
}
6489

@@ -75,20 +100,32 @@ TEST_CASE("Valid OTA data is received ", "[OTALogic]")
75100

76101
TEST_CASE("Invalid OTA data is received ", "[OTALogic - CRC wrong]")
77102
{
78-
OTAStorage_Mock ota_storage;
79-
OTALogic ota_logic(ota_storage);
80-
OTAData invalid_valid_ota_test_data_crc_wrong;
103+
Mock<OTAStorage> ota_storage;
104+
81105

82-
ota_storage._init_return_val = true;
83-
ota_storage._open_return_val = true;
106+
/* Configure mock object */
107+
When(Method(ota_storage, init)).Return(true);
108+
When(Method(ota_storage, open)).Return(true);
109+
When(Method(ota_storage, write)).AlwaysDo([](uint8_t const * const /* buf */, size_t const num_bytes) -> size_t { return num_bytes; });
110+
Fake(Method(ota_storage, close));
111+
Fake(Method(ota_storage, remove));
112+
Fake(Method(ota_storage, deinit));
84113

114+
115+
/* Generate test data */
116+
OTAData invalid_valid_ota_test_data_crc_wrong;
85117
generate_invalid_ota_data_crc_wrong(invalid_valid_ota_test_data_crc_wrong);
86118

119+
120+
/* Perform test */
121+
OTALogic ota_logic(ota_storage.get());
87122
simulateOTABinaryReception(ota_logic, invalid_valid_ota_test_data_crc_wrong);
88123

124+
125+
/* Perform checks */
89126
THEN("there should be no binary file be stored on the OTA storage")
90127
{
91-
REQUIRE(ota_storage._binary.size() == 0);
128+
Verify(Method(ota_storage, remove)).Once();
92129
}
93130

94131
THEN("The OTA logic should be in the 'Error' state")

0 commit comments

Comments
 (0)