FabioBatSilva / ArduinoFake

Arduino mocking made easy
https://platformio.org/lib/show/1689/ArduinoFake
MIT License
102 stars 47 forks source link

About Serial.print() and Serial1.print() in ESP32 projects #52

Open joellozano30 opened 12 months ago

joellozano30 commented 12 months ago

I'm starting a embedded project wich uses ESP32 microcontroller using the Arduino Framework and some modules like a Sigfox Module. I want to test a function which send messages via Sigfox:

void sigfoxSendMsg(String buf_tx)
{
    //Adding line break
    buf_tx += "\n";

    //******************************
    //Enable Sigfox Module
    digitalWrite(SIGFOX_ENABLE, HIGH);
    delay(1000);

    #ifndef TEST
    //Channel reset to ensure correct frequency
    Serial1.print("AT$RC\n");

    //******************************
    //Sending data on Sigfox
    Serial1.print(buf_tx);
    #else

    Serial.print("AT$RC\n");
    Serial.print(buf_tx);
    #endif

    #ifdef SIGFOX_DEBUG
        #ifndef TEST
        Serial.print(buf_tx);
        #endif
    #endif
    delay(3000);

    //disable sigfox module
    digitalWrite(SIGFOX_ENABLE, LOW);
}

But when I test it with my test function:

void test_sigfoxSendMsg_should_beAbleTo_SendMessagesCorrectly(){
    String packedMessage = "AT$SF=d63c1042"; //Data

    When(Method(ArduinoFake(), digitalWrite)).AlwaysReturn();
    When(Method(ArduinoFake(), delay)).AlwaysReturn();

    When(OverloadedMethod(ArduinoFake(Serial), print, size_t(char))).AlwaysReturn();

    sigfoxSendMsg(packedMessage);

    Verify(Method(ArduinoFake(), digitalWrite).Using(SIGFOX_ENABLE, HIGH));
    Verify(Method(ArduinoFake(), digitalWrite).Using(SIGFOX_ENABLE, LOW));
}

The result of the test show me how the function Serial.print() wasn't mocked:

Building...
Testing...
Program errored with 3221225477 code

How can i fix these errors? I believe that I'am mocking Serial.print() wrongly. Additionaly, there is a way to use Serial1.print() instead of Serial.print() with ArduinoFake? It would help me to other functions that I want to adapt to test also.

FabioBatSilva commented 11 months ago

If you run with --verbose should get a bit more info platformio test -e native -v But the error is likely BC Serial1 is not implemented.

There are some some instructions here https://github.com/FabioBatSilva/ArduinoFake/blob/master/CONTRIBUTING.md on how to add in case you wanna give it a go.

I think for this one is mostly reusing the what is already in place for Serial