Mocked object not passing to the other class
I'm making a mock class of istream to test a class. But the mocked object is not passing to the actual class i want to use in.
This is the mock class.
class MockStream{
private:
std::string filename_;
public:
MockStream(){
}
MockStream(std::string filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out):filename_(filename){
}
MockStream(const MockStream &a){}
MockStream& operator=(const MockStream& a){
return *this;
}
~MockStream(){}
MOCK_CONST_METHOD0(is_open,bool());
MOCK_METHOD0(close,void());
MOCK_METHOD2(read,MockStream&(char*,std::streamsize));
MOCK_METHOD2(write,MockStream&(const char*,std::streamsize));
MOCK_METHOD2(seekg,MockStream&(std::streamoff,std::ios_base::seekdir));
MOCK_METHOD1(seekg,MockStream&(std::streampos));
MOCK_METHOD0(get_filename,std::string());
};
test:
TEST(random_access_stack, mocked_test)
{
RandomAccessStack<TestClass,u_int64_t,MockStream> stack;
MockStream mocked;
EXPECT_CALL(mocked,is_open()).Times(1).WillOnce(testing::Return(true));
stack.SetStream(mocked);
EXPECT_TRUE(stack.is_open());
}
SetStream function:
void SetStream(STREAM& handle){
file_handle_ = handle;
}
where file_handle_ is of type MockStream passed by template
The output is:
GMOCK WARNING:
Uninteresting mock function call - returning default value.
Function call: is_open()
Returns: false
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details.
/build/libs/storage/tests/gtest/random_access_stack_tests.cpp:79: Failure
Value of: stack.is_open()
Actual: false
Expected: true
/build/libs/storage/tests/gtest/random_access_stack_tests.cpp:73: Failure
Actual function call count doesn't match EXPECT_CALL(mocked, is_open())...
Expected: to be called once
Actual: never called - unsatisfied and active
c++ googletest googlemock gmock
New contributor
add a comment |
I'm making a mock class of istream to test a class. But the mocked object is not passing to the actual class i want to use in.
This is the mock class.
class MockStream{
private:
std::string filename_;
public:
MockStream(){
}
MockStream(std::string filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out):filename_(filename){
}
MockStream(const MockStream &a){}
MockStream& operator=(const MockStream& a){
return *this;
}
~MockStream(){}
MOCK_CONST_METHOD0(is_open,bool());
MOCK_METHOD0(close,void());
MOCK_METHOD2(read,MockStream&(char*,std::streamsize));
MOCK_METHOD2(write,MockStream&(const char*,std::streamsize));
MOCK_METHOD2(seekg,MockStream&(std::streamoff,std::ios_base::seekdir));
MOCK_METHOD1(seekg,MockStream&(std::streampos));
MOCK_METHOD0(get_filename,std::string());
};
test:
TEST(random_access_stack, mocked_test)
{
RandomAccessStack<TestClass,u_int64_t,MockStream> stack;
MockStream mocked;
EXPECT_CALL(mocked,is_open()).Times(1).WillOnce(testing::Return(true));
stack.SetStream(mocked);
EXPECT_TRUE(stack.is_open());
}
SetStream function:
void SetStream(STREAM& handle){
file_handle_ = handle;
}
where file_handle_ is of type MockStream passed by template
The output is:
GMOCK WARNING:
Uninteresting mock function call - returning default value.
Function call: is_open()
Returns: false
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details.
/build/libs/storage/tests/gtest/random_access_stack_tests.cpp:79: Failure
Value of: stack.is_open()
Actual: false
Expected: true
/build/libs/storage/tests/gtest/random_access_stack_tests.cpp:73: Failure
Actual function call count doesn't match EXPECT_CALL(mocked, is_open())...
Expected: to be called once
Actual: never called - unsatisfied and active
c++ googletest googlemock gmock
New contributor
where do you expectmocked.is_open()
to be called? insidestack.is_open()
?
– Mike van Dyke
2 days ago
@MikevanDyke yes. Copying the mocked object infile_handle_
, then calling itsis_open()
instack.is_open()
– Talal Irfan
yesterday
andfile_handle_
is of typeMockStream
or is it a reference to theMockStream
-objectmocked
?
– Mike van Dyke
yesterday
add a comment |
I'm making a mock class of istream to test a class. But the mocked object is not passing to the actual class i want to use in.
This is the mock class.
class MockStream{
private:
std::string filename_;
public:
MockStream(){
}
MockStream(std::string filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out):filename_(filename){
}
MockStream(const MockStream &a){}
MockStream& operator=(const MockStream& a){
return *this;
}
~MockStream(){}
MOCK_CONST_METHOD0(is_open,bool());
MOCK_METHOD0(close,void());
MOCK_METHOD2(read,MockStream&(char*,std::streamsize));
MOCK_METHOD2(write,MockStream&(const char*,std::streamsize));
MOCK_METHOD2(seekg,MockStream&(std::streamoff,std::ios_base::seekdir));
MOCK_METHOD1(seekg,MockStream&(std::streampos));
MOCK_METHOD0(get_filename,std::string());
};
test:
TEST(random_access_stack, mocked_test)
{
RandomAccessStack<TestClass,u_int64_t,MockStream> stack;
MockStream mocked;
EXPECT_CALL(mocked,is_open()).Times(1).WillOnce(testing::Return(true));
stack.SetStream(mocked);
EXPECT_TRUE(stack.is_open());
}
SetStream function:
void SetStream(STREAM& handle){
file_handle_ = handle;
}
where file_handle_ is of type MockStream passed by template
The output is:
GMOCK WARNING:
Uninteresting mock function call - returning default value.
Function call: is_open()
Returns: false
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details.
/build/libs/storage/tests/gtest/random_access_stack_tests.cpp:79: Failure
Value of: stack.is_open()
Actual: false
Expected: true
/build/libs/storage/tests/gtest/random_access_stack_tests.cpp:73: Failure
Actual function call count doesn't match EXPECT_CALL(mocked, is_open())...
Expected: to be called once
Actual: never called - unsatisfied and active
c++ googletest googlemock gmock
New contributor
I'm making a mock class of istream to test a class. But the mocked object is not passing to the actual class i want to use in.
This is the mock class.
class MockStream{
private:
std::string filename_;
public:
MockStream(){
}
MockStream(std::string filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out):filename_(filename){
}
MockStream(const MockStream &a){}
MockStream& operator=(const MockStream& a){
return *this;
}
~MockStream(){}
MOCK_CONST_METHOD0(is_open,bool());
MOCK_METHOD0(close,void());
MOCK_METHOD2(read,MockStream&(char*,std::streamsize));
MOCK_METHOD2(write,MockStream&(const char*,std::streamsize));
MOCK_METHOD2(seekg,MockStream&(std::streamoff,std::ios_base::seekdir));
MOCK_METHOD1(seekg,MockStream&(std::streampos));
MOCK_METHOD0(get_filename,std::string());
};
test:
TEST(random_access_stack, mocked_test)
{
RandomAccessStack<TestClass,u_int64_t,MockStream> stack;
MockStream mocked;
EXPECT_CALL(mocked,is_open()).Times(1).WillOnce(testing::Return(true));
stack.SetStream(mocked);
EXPECT_TRUE(stack.is_open());
}
SetStream function:
void SetStream(STREAM& handle){
file_handle_ = handle;
}
where file_handle_ is of type MockStream passed by template
The output is:
GMOCK WARNING:
Uninteresting mock function call - returning default value.
Function call: is_open()
Returns: false
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details.
/build/libs/storage/tests/gtest/random_access_stack_tests.cpp:79: Failure
Value of: stack.is_open()
Actual: false
Expected: true
/build/libs/storage/tests/gtest/random_access_stack_tests.cpp:73: Failure
Actual function call count doesn't match EXPECT_CALL(mocked, is_open())...
Expected: to be called once
Actual: never called - unsatisfied and active
c++ googletest googlemock gmock
c++ googletest googlemock gmock
New contributor
New contributor
edited 2 days ago
New contributor
asked 2 days ago
Talal Irfan
13
13
New contributor
New contributor
where do you expectmocked.is_open()
to be called? insidestack.is_open()
?
– Mike van Dyke
2 days ago
@MikevanDyke yes. Copying the mocked object infile_handle_
, then calling itsis_open()
instack.is_open()
– Talal Irfan
yesterday
andfile_handle_
is of typeMockStream
or is it a reference to theMockStream
-objectmocked
?
– Mike van Dyke
yesterday
add a comment |
where do you expectmocked.is_open()
to be called? insidestack.is_open()
?
– Mike van Dyke
2 days ago
@MikevanDyke yes. Copying the mocked object infile_handle_
, then calling itsis_open()
instack.is_open()
– Talal Irfan
yesterday
andfile_handle_
is of typeMockStream
or is it a reference to theMockStream
-objectmocked
?
– Mike van Dyke
yesterday
where do you expect
mocked.is_open()
to be called? inside stack.is_open()
?– Mike van Dyke
2 days ago
where do you expect
mocked.is_open()
to be called? inside stack.is_open()
?– Mike van Dyke
2 days ago
@MikevanDyke yes. Copying the mocked object in
file_handle_
, then calling its is_open()
in stack.is_open()
– Talal Irfan
yesterday
@MikevanDyke yes. Copying the mocked object in
file_handle_
, then calling its is_open()
in stack.is_open()
– Talal Irfan
yesterday
and
file_handle_
is of type MockStream
or is it a reference to the MockStream
-object mocked
?– Mike van Dyke
yesterday
and
file_handle_
is of type MockStream
or is it a reference to the MockStream
-object mocked
?– Mike van Dyke
yesterday
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Talal Irfan is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945291%2fmocked-object-not-passing-to-the-other-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Talal Irfan is a new contributor. Be nice, and check out our Code of Conduct.
Talal Irfan is a new contributor. Be nice, and check out our Code of Conduct.
Talal Irfan is a new contributor. Be nice, and check out our Code of Conduct.
Talal Irfan is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945291%2fmocked-object-not-passing-to-the-other-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
where do you expect
mocked.is_open()
to be called? insidestack.is_open()
?– Mike van Dyke
2 days ago
@MikevanDyke yes. Copying the mocked object in
file_handle_
, then calling itsis_open()
instack.is_open()
– Talal Irfan
yesterday
and
file_handle_
is of typeMockStream
or is it a reference to theMockStream
-objectmocked
?– Mike van Dyke
yesterday