'(也许你打算在gmock test

时间:2016-09-01 14:41:48

标签: c++ pointers googletest googlemock

我使用gtest和gmock进行单元测试。

这些是提供的,而我应该让它们通过。

我有一个RentalAdministration对象,它采用(在某些方法中)Car对象指针。其中一个功能如下:

bool RentalAdministration::Add(Car *car){
    if(!car){
         throw invalid_argument("Car parameter can't be null!");
    }

    if(FindCar(car->GetLicencePlate()) != NULL){
         return false;
    }

    cars.push_back(car);
    return true;
}

其中一项测试看起来像是他的:

TEST(RentalAdministrationAdd, test_add_car_to_empty_administration)
{
    RentalAdministration admin;
    Car *car;
    EXPECT_CALL(car, GetLicencePlate()).WillOnce(Return("fh-01-ict"));
    EXPECT_TRUE(admin.Add(&car));
}

当我尝试进行这些测试时,我会遇到很多错误,所有错误都是相同的:

g++ -Wall -Werror -pedantic -ggdb -O0 -std=c++98 -Iproduct -Itest test/RentalAdministrationTest.cpp product/RentalAdministration.cpp -lgtest -lgmock -lgmock_main -lpthread -o RentalAdministrationTest
In file included from /usr/include/gmock/gmock-generated-function-mockers.h:43:0,
                 from /usr/include/gmock/gmock.h:61,
                 from test/RentalAdministrationTest.cpp:2:
test/RentalAdministrationTest.cpp: In member function ‘virtual void RentalAdministrationAdd_test_add_car_to_empty_administration_Test::TestBody()’:
test/RentalAdministrationTest.cpp:13:5: error: request for member ‘gmock_GetLicencePlate’ in ‘car’, which is of pointer type ‘Car*’ (maybe you meant to use ‘->’ ?)
     EXPECT_CALL(car, GetLicencePlate()).WillOnce(Return("fh-01-ict"));
     ^
In file included from /usr/include/gtest/gtest.h:58:0,
                 from /usr/include/gmock/internal/gmock-internal-utils.h:47,
                 from /usr/include/gmock/gmock-actions.h:46,
                 from /usr/include/gmock/gmock.h:58,
                 from test/RentalAdministrationTest.cpp:2:
test/RentalAdministrationTest.cpp:14:31: error: no matching function for call to ‘RentalAdministration::Add(Car**)’
     EXPECT_TRUE(admin.Add(&car));

^

我想知道我在这里做错了什么。我刚刚开始使用C ++。

btw:我正在使用g ++编译并拥有版本98。

感谢。

编辑:请原谅我没有张贴这个,但我有一个Car.h的模拟文件,它与测试本身在同一个文件夹中。

#ifndef __CAR_H
#define __CAR_H

#include "gmock/gmock.h"

#include <string>
using namespace std;

class Car
{
    public:
        MOCK_CONST_METHOD0(GetLicencePlate, string());
        MOCK_METHOD0(Rent, bool());
        MOCK_METHOD1(Return, double(int kilometers));
        MOCK_METHOD0(Clean, void());
};


#endif

Edit1:正如我所建议的那样,我试图使用Car *的引用(&amp;)。 这会导致我上面提到的错误,但是还有其他一些错误:

 error: cannot declare field ‘AdminTest::car’ to be of abstract type ‘Car’
     Car car;

 error: ‘class Car’ has no member named ‘gmock_GetLicencePlate’

3 个答案:

答案 0 :(得分:3)

car是指向Car的指针。它的类型是:Car*。要从Car调用Car*成员,您必须取消引用指针。使用car->x语法时会自动完成此操作,该语法相当于(*car).x

我假设EXPECT_CALL需要一个非指针参数。因此,您可以获得car的引用并将其传递给宏:

Car& car_ref = *car;
EXPECT_CALL(car_ref, GetLicencePlate()).WillOnce(Return("fh-01-ict"));

答案 1 :(得分:1)

首先是指针问题,维托里奥已经说过,但是,你还需要模拟Car对象使用EXCEPT_CALL:

 error: function (jqXHR, textStatus, errorThrown) {
    if(jqXHR.status === 413) {
        // Request entity too large
        // To solve this we split the data we want to upload into several smaller partitions
        // and upload them sequentially

        console.log("Error 413: Request entity too large. Splitting the data into partitions...");

        // handling code below

        // blahblahblah
    }
},

你可以在我的考试中看到你没有使用真正的汽车对象,而是一个可以控制访问并强制返回值的模拟

答案 2 :(得分:0)

我找到了解决方案。

对于任何想知道的人:在一个班级(RentalAdministration)中,我已经包含Car.h文件夹中的cars文件。 (如#include "cars/Car.h"

但是,对于测试,应使用不同的Car.h。因此,更改为#include "Car.h"会使其正常工作。

相关问题