Unit testing with juce::Value::Listener fails

Hello,
I have the following problem: I am using catch2 as a unit testing framework and I am trying to test my class, that derives from juce::Value::Listener. I’ve used the testing framework with juce a lot and it works fine in general. I have learned to call ScopedJuceInitialiser_GUI() to get the MessageManager to run, but somehow the code - as outline/simplified below - still does not work. With some investigation I found listeners are called asynchronously, but even if I throw-in a sleep/wait after changing the value, the listener still doesn’t get called. Any help much appreciated.

#include <catch2/catch_all.hpp>

class MyListener : public juce::Value::Listener
{
public:
void valueChanged (Value& value)
{
changed = true;
}

bool valueHasChanged()
{
    return changed;
}

private:
bool changed {false};
};

TEST_CASE (“Test”, “[Test]”)
{
const auto x = juce::ScopedJuceInitialiser_GUI();

auto value = juce::Value (var (0));

auto listener = MyListener();
value.addListener (&listener);

value.setValue (1);

REQUIRE (listener.valueHasChanged());

}

I found the answer to this problem here:

You need to run the test-runner on a new thread, and then run the core of the actual test on the main/message-thread. For a more detailed explanation follow the link above.

Beat me to it, I couldn’t find that thread when I searched. We’ll try and put a tutorial together some point in the future, post JUCE 8.

1 Like