Reputation: 1343
I was trying to make a minimal gmock test case from donsoft.io's example
The file structure is simple:
my_workspace/
├── BUILD
├── WORKSPACE
├── coinflipper.cc
├── coinflipper.h
├── mockrng.cc
├── mockrng.h
└── rng.h
I got this error while trying to compile by $ bazel test --test_output=all //:mockrng
$ bazel test --test_output=all //:mockrng
INFO: Analyzed target //:mockrng (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
FAIL: //:mockrng (see /private/var/tmp/_bazel_pvd/83a142a3c9781856c99d4f0c60c1af76/execroot/__main__/bazel-out/darwin-fastbuild/testlogs/mockrng/test.log)
INFO: From Testing //:mockrng:
==================== Test output for //:mockrng:
dyld: Symbol not found: __ZTI3Rng
Referenced from: /private/var/tmp/_bazel_pvd/83a142a3c9781856c99d4f0c60c1af76/sandbox/darwin-sandbox/21/execroot/__main__/bazel-out/darwin-fastbuild/bin/mockrng.runfiles/__main__/mockrng
Expected in: flat namespace
in /private/var/tmp/_bazel_pvd/83a142a3c9781856c99d4f0c60c1af76/sandbox/darwin-sandbox/21/execroot/__main__/bazel-out/darwin-fastbuild/bin/mockrng.runfiles/__main__/mockrng
================================================================================
Target //:mockrng up-to-date:
bazel-bin/mockrng
INFO: Elapsed time: 0.256s, Critical Path: 0.13s
INFO: 2 processes: 2 darwin-sandbox.
INFO: Build completed, 1 test FAILED, 2 total actions
//:mockrng FAILED in 0.1s
/private/var/tmp/_bazel_pvd/83a142a3c9781856c99d4f0c60c1af76/execroot/__main__/bazel-out/darwin-fastbuild/testlogs/mockrng/test.log
INFO: Build completed, 1 test FAILED, 2 total actions
My code were basically the same as donsoft.io, just wondering if there anything that I had missed?
#include "coinflipper.h"
CoinFlipper::CoinFlipper(Rng* rng) : d_rng(rng) {}
CoinFlipper::Result CoinFlipper::flipCoin() const {
const double val = d_rng->generate(0.0, 1.0);
return (val < 0.5) ? HEADS : TAILS;
}
#include "rng.h"
class CoinFlipper {
private:
Rng* d_rng; // held, not owned
public:
enum Result { HEADS = 0, TAILS = 1 };
explicit CoinFlipper(Rng* rng);
Result flipCoin() const;
};
#include "mockrng.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "coinflipper.h"
TEST(CoinFlipper, ShouldReturnHeadsIfRandValueIsLessThanProbability) {
MockRng rng;
EXPECT_CALL(rng, generate(::testing::DoubleEq(0.0), ::testing::DoubleEq(1.0)))
.Times(::testing::Exactly(1))
.WillOnce(::testing::Return(0.25));
CoinFlipper coinFlipper(&rng);
CoinFlipper::Result result = coinFlipper.flipCoin();
EXPECT_EQ(CoinFlipper::HEADS, result);
}
#include "gmock/gmock.h"
#include "rng.h"
class MockRng : public Rng {
public:
MOCK_METHOD2(generate, double(double, double));
};
#ifndef RNG_H
#define RNG_H
class Rng {
public:
virtual ~Rng();
virtual double generate(double min, double max) = 0;
};
#endif
load("@rules_cc//cc:defs.bzl", "cc_test")
cc_test(
name = "mockrng",
size = "small",
srcs = ["mockrng.cc", "mockrng.h", "rng.h","coinflipper.h","coinflipper.cc"],
deps = ["@com_google_googletest//:gtest_main"],
)
Upvotes: 0
Views: 109
Reputation: 6326
The definition part for Rng::~Rng()
is missing, a slightly fix will work:
#ifndef RNG_H
#define RNG_H
class Rng {
public:
virtual ~Rng() {};
virtual double generate(double min, double max) = 0;
};
#endif
By the way, what version of the compiler are you using? A modern compiler will give us explicit error message:
undefined reference to `Rng::~Rng()'
Rather than the one you get:
dyld: Symbol not found: __ZTI3Rng
Some irrelevant suggestions: Some header files are missing a guard processor, it's error-prone and needs to be fixed.
Upvotes: 1