Hello, I’m searching mocking library for Arduino. I need something like fakeit or gmock, but compatible with Arduino.
The official PlatformIO + Arduino mocking example uses the ArduinoFake library which is exactly based on FakeIt. This repo is also referenced in the PIO Unit Testing – Project Examples section.
I tried to setup fakeIt, but I got an error: can’t open “functional” file(dependency of “single_header/standalone/fakeit.hpp”) I think that I can’t because I think that fakeit was written for native platform.
Platformie.ini: I put this line into build_flags: -I"${PROJECT_LIBDEPS_DIR}/FakeIt/"
You should use the ArduinoFake
library as intended in Unit test mode and through ArduinoFake.h
which auto-includes FakeIt
The raw FakeIt library (GitHub - eranpeer/FakeIt: C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.) is not PlatformIO-ready since it has a non-standard folder structure (include file is in a folder called single_header
) and there’s no library.json
.
If I recreate the above mocking example with platformio.ini
[env:native]
platform = native
build_flags = -std=gnu++11
lib_deps =
ArduinoFake
and test\test_main.cpp
(not in src/
) with
#include <Arduino.h> //redirects to ArduinoFake.h in this case
#include <unity.h>
#include <string>
#include <fakeit/fakeit.hpp>
using namespace fakeit;
void setUp(void)
{
ArduinoFakeReset();
}
struct SomeInterface {
virtual int foo(int) = 0;
virtual int bar(std::string) = 0;
};
void test_func_expected_pass(void)
{
Mock<SomeInterface> mock;
When(Method(mock,foo)).Return(0);
SomeInterface &i = mock.get();
// Production code
i.foo(1);
// Verify method mock.foo was invoked with specific arguments.
Verify(Method(mock,foo).Using(1));
}
void test_func_expected_fail(void)
{
Mock<SomeInterface> mock;
When(Method(mock,foo)).Return(0);
SomeInterface &i = mock.get();
// Production code
i.foo(2);
// Verify method mock.foo was invoked with specific arguments.
Verify(Method(mock,foo).Using(1));
}
int main(int argc, char **argv)
{
UNITY_BEGIN();
RUN_TEST(test_func_expected_pass);
RUN_TEST(test_func_expected_fail);
return UNITY_END();
}
I can use FakeIt directly and the results are as expected (1 pass, 1 fail) when using Advanced -> Test
.
So Fakeit work only in native environment(in platformo)?
You want to run ArduinoFake or FakeIt on an embedded device?
Yes, I want to run FakiIt on an embedded device. I want to mock Serial, because I don’t want to connect external device to print something to serial.
I see, this is a bit more complicated. To get the embedded device running nicely without doing laberous startup-work yourself one should use framework = arduino
, but then the Serial implementation is there and likely can’t be mocked anymore without conflicting with the real implementation – one may work around this by using a either baremetal framework or a different framework that does not include Arduino, and then include ArduinoFake (with lib_compat_mode
turned off) to pull in the Arduino mocks. What exact microcontroller are you running on?
Arduino Uno/nano. In my code I inject serial object to class that use it, so that is not big problem I think.