How to mock nested methods/attributes

182 views
Skip to first unread message

Jason Dunsmore

unread,
Apr 2, 2013, 1:48:10 PM4/2/13
to mox-d...@googlegroups.com
Hi,

I'm trying to mock a call to git.Repo(path).remotes.origin.pull().  Here's one of my many attempts:

        self.mox.StubOutWithMock(git, 'Repo')
        repo = git.Repo(IgnoreArg())
        repo_remotes = self.mox.CreateMockAnything()
        repo_remotes_origin = self.mox.CreateMockAnything()
        repo.remotes.AndReturn(repo_remotes)
        repo_remotes.origin.AndReturn(repo_remotes_origin)
        repo_remotes_origin.pull().AndReturn(True)
        self.mox.ReplayAll()

The error I see is:

"AttributeError: MockMethod has no attribute "remotes". Did you remember to put your mocks in replay mode?"

Can anybody provide some guidance?  I'm new to object mocking frameworks.

Thanks,
Jason

Jason D

unread,
Apr 2, 2013, 3:19:16 PM4/2/13
to mox-d...@googlegroups.com
FYI, it seems like the following will work:

        self.mox.StubOutWithMock(git, 'Repo')
        mock_repo = self.mox.CreateMockAnything()
        git.Repo(IgnoreArg()).AndReturn(mock_repo)
        repo_remotes = self.mox.CreateMockAnything()
        repo_remotes_origin = self.mox.CreateMockAnything()
        setattr(mock_repo, "remotes", repo_remotes)
        setattr(repo_remotes, "origin", repo_remotes_origin)
        repo_remotes_origin.pull()
        self.mox.ReplayAll()

Jason D

unread,
Apr 3, 2013, 10:44:26 AM4/3/13
to mox-d...@googlegroups.com
Apparently, this isn't quite right.  When mox.VerifyAll() is called, I get the following error:

ExpectedMethodCallsError: Verify: Expected methods never called:
  0.  Repo.__call__(<IgnoreArg>) -> <MockAnything instance>

Any ideas?
Reply all
Reply to author
Forward
0 new messages