This is related to the idea of "don't mock code you don't own".
On Sun, Apr 12, 2009 at 10:02 PM, kalkotivinay <kalkot...@gmail.com> wrote:
--
C. Keith Ray, IXP Coach, Industrial Logic, Inc.
http://industriallogic.com 866-540-8336 (toll free)
Groove with our Agile Greatest Hits: http://www.industriallogic.com/elearning/
http://agilesolutionspace.blogspot.com/
You are defining class syscall in both syscall.hpp and
mocksyscall.hpp. If you use both headers in the same program, you
violate the one-definition rule and your code is invalid.
> class MockSysCall : public syscall
> {
>
> public:
> MOCK_METHOD2( Open, int(const char *pathname, int flags ));
> };
I would remove class syscall from mocksyscall.hpp and write this
instead:
class MockSysCall : public syscallinterface { ... };
> In my foo class, I am trying to test createfoo method which is using
> syscall::Open().
>
> // FooTest.cpp
>
> mocksyscall mocksystemcall;
> EXPECT_CALL( mocksystemcall, System(_)).WillRepeatedly(Return(-1));
Is System(_) a typo? I thought you want Open().
> int status = foo::createfoo();
>
> I am expecting createfoo() to return -1 for syscall::Open() and it is
> not.
> Looks like its not even calling the mock method.
Try running your program with the --gmock_verbose=info command-line
flag. It tells you which mock methods are called.
> Can anyone help me on this.
>
> Thanks,
> Vinay
>
> On Apr 14, 12:17 am, Keith Ray <keith....@gmail.com> wrote:
>> I suggest introducing an adapter for those system calls (create a
>> class that handles open/read/write/close) and mock the interface of
>> that adapter when testing code that uses that adapter.
>>
>> This is related to the idea of "don't mock code you don't own".
>>
>> On Sun, Apr 12, 2009 at 10:02 PM, kalkotivinay <kalkoti.vi...@gmail.com> wrote:
>> > I am using google mock to unit my code. I have used some system calls
>> > in my code like open(), read() and write() and I do want to mock these
>> > system calls.
>>
>> > Please let me know how can I do this ?
>>
>> --
>> C. Keith Ray, IXP Coach, Industrial Logic, Inc.http://industriallogic.com 866-540-8336 (toll free)
>> Groove with our Agile Greatest Hits:http://www.industriallogic.com/elearning/http://agilesolutionspace.blogspot.com/
>
--
Zhanyong
It appears that your mocksyscall class missed some pure virtual
methods in syscallinterface. Please show us your real code.
--
Zhanyong
BTW, you should have
virtual ~SysCallInterface() {}
here as the class is meant to be inherited from.
> virtual int Open(const char *pathname, int flags) = 0;
> };
>
>
> // MockSysCall.hpp
>
> #include "../hdr/SysCallInterface.hpp"
>
> class MockSysCall : public SysCallInterface
> {
>
> public:
> MOCK_METHOD2( Open, int(const char *pathname, int flags ));
> };
>
> // FooTest.cpp
> #include "MockSysCall.hpp
>
> class FooTest : public testing::Test
> {
> // some attributes.
> virtual void SetUp() { ... }
> };
>
> TEST_F( FooTest, createfoo )
> {
> MockSysCall mocksystemcall;
> SysCallInterface *sc;
> sc = &mocksystemcall;
>
> EXPECT_CALL( *sc, Open(_)).WillRepeatedly(Return(-1));
Use mocksystemcall instead of *sc in this statement. The first
argument to EXPECT_CALL must be a mock object.
--
Zhanyong
What is 'mocksyscall'? I only see MockSysCall in your code. Is this
the real error message? Please post the real message. Please also
post the version and name of your compiler.
> Does gmock define any function for Open when I use MOCK_METHODD() ?.
Yes, it implements Open.
--
Zhanyong
I have to change Open(_) to Open(_, _). Then I can compile it using gcc 4.
Do you have a macro "#define Open(x, y) ...." somewhere? Can you try
to rename Open() to something different?
--
Zhanyong
On Wed, May 13, 2009 at 12:27 AM, kalkotivinay <kalkot...@gmail.com> wrote:
>
>
> Hi,
>
> They are some typos. copy-paste is not working on my machine for some
> reason.
It's important we are looking at the actual code. Could you reduce
your code to the bare minimum that shows the problem (ideally in one
file) and attach your file?
> I am using Open(_,_) and there are no #defines for Open.
Have you tried to rename Open() to something else as suggested? If
that fixed the problem, you might have a #define Open(...) in some
header you #include.
--
Zhanyong
Your function under test is not using a mock object but rather instantiating a real one (i.e. SysCall). You will need to break the dependency in this function so that you can pass in or otherwise use an instance of MockSysCall instead.
Regards,
Bruce
--- On Wed, 5/13/09, kalkotivinay <kalkot...@gmail.com> wrote:
Regards,
Bruce
--- On Thu, 5/14/09, kalkotivinay <kalkot...@gmail.com> wrote: