Skip to content

Commit da4db6a

Browse files
committed
mock injection example
1 parent c94dd35 commit da4db6a

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed

examples/mock-injection/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.pioenvs
2+
.piolibdeps
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Continuous Integration (CI) is the practice, in software
2+
# engineering, of merging all developer working copies with a shared mainline
3+
# several times a day < http://docs.platformio.org/page/ci/index.html >
4+
#
5+
# Documentation:
6+
#
7+
# * Travis CI Embedded Builds with PlatformIO
8+
# < https://docs.travis-ci.com/user/integration/platformio/ >
9+
#
10+
# * PlatformIO integration with Travis CI
11+
# < http://docs.platformio.org/page/ci/travis.html >
12+
#
13+
# * User Guide for `platformio ci` command
14+
# < http://docs.platformio.org/page/userguide/cmd_ci.html >
15+
#
16+
#
17+
# Please choice one of the following templates (proposed below) and uncomment
18+
# it (remove "# " before each line) or use own configuration according to the
19+
# Travis CI documentation (see above).
20+
#
21+
22+
23+
#
24+
# Template #1: General project. Test it using existing `platformio.ini`.
25+
#
26+
27+
# language: python
28+
# python:
29+
# - "2.7"
30+
#
31+
# sudo: false
32+
# cache:
33+
# directories:
34+
# - "~/.platformio"
35+
#
36+
# install:
37+
# - pip install -U platformio
38+
#
39+
# script:
40+
# - platformio run
41+
42+
43+
#
44+
# Template #2: The project is intended to by used as a library with examples
45+
#
46+
47+
# language: python
48+
# python:
49+
# - "2.7"
50+
#
51+
# sudo: false
52+
# cache:
53+
# directories:
54+
# - "~/.platformio"
55+
#
56+
# env:
57+
# - PLATFORMIO_CI_SRC=path/to/test/file.c
58+
# - PLATFORMIO_CI_SRC=examples/file.ino
59+
# - PLATFORMIO_CI_SRC=path/to/test/directory
60+
#
61+
# install:
62+
# - pip install -U platformio
63+
#
64+
# script:
65+
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[env:native]
2+
platform = native
3+
build_flags = -std=gnu++11
4+
lib_deps = [email protected]:FabioBatSilva/ArduinoFake.git#cmake
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "MyService.h"
2+
3+
MyService::MyService(Client* client)
4+
{
5+
_client = client;
6+
}
7+
8+
String MyService::request(const char *server) {
9+
String response;
10+
11+
if (_client->connect(server, 80)) {
12+
_client->println("STATUS");
13+
_client->println();
14+
}
15+
16+
while(_client->available()) {
17+
response.concat(_client->read());
18+
}
19+
20+
_client->stop();
21+
22+
return response;
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <Arduino.h>
4+
#include <Client.h>
5+
6+
class MyService
7+
{
8+
public:
9+
MyService (Client* client);
10+
String request(const char* url);
11+
12+
private:
13+
Client* _client;
14+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <Arduino.h>
2+
#include <unity.h>
3+
4+
using namespace fakeit;
5+
6+
#include "MyService.h"
7+
8+
void setUp(void)
9+
{
10+
ArduinoFakeReset();
11+
}
12+
13+
void test_connect(void)
14+
{
15+
When(Method(ArduinoFake(Client), stop)).AlwaysReturn();
16+
When(Method(ArduinoFake(Client), available)).Return(1, 1, 1, 0);
17+
When(OverloadedMethod(ArduinoFake(Client), read, int())).Return(2, 0, 0);
18+
When(OverloadedMethod(ArduinoFake(Client), println, size_t())).AlwaysReturn();
19+
When(OverloadedMethod(ArduinoFake(Client), println, size_t(const char *))).AlwaysReturn();
20+
When(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t))).Return(1);
21+
22+
Client* clientMock = ArduinoFakeMock(Client);
23+
24+
MyService service(clientMock);
25+
26+
String response = service.request("myserver.com");
27+
28+
TEST_ASSERT_EQUAL(3, response.length());
29+
TEST_ASSERT_TRUE(response.equals("200"));
30+
31+
Verify(Method(ArduinoFake(Client), stop)).Once();
32+
Verify(Method(ArduinoFake(Client), available)).Exactly(4_Times);
33+
Verify(OverloadedMethod(ArduinoFake(Client), read, int())).Exactly(3_Times);
34+
35+
Verify(OverloadedMethod(ArduinoFake(Client), println, size_t())).Once();
36+
Verify(OverloadedMethod(ArduinoFake(Client), println, size_t(const char *)).Using("STATUS")).Once();
37+
Verify(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t)).Using("myserver.com", 80)).Once();
38+
}
39+
40+
int main(int argc, char **argv)
41+
{
42+
UNITY_BEGIN();
43+
44+
RUN_TEST(test_connect);
45+
46+
return UNITY_END();
47+
}

0 commit comments

Comments
 (0)