Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8542445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:04:25+00:00 2026-06-11T12:04:25+00:00

I would like to patch an attribute of the data returned by a method.

  • 0

I would like to patch an attribute of the data returned by a method.

Assuming I have the following simplified piece code:

@patch('requests.post')
class TestKeywordsApi(BaseTest):
    # Instantiate API class and set the apikey
    def setUp(self):
        BaseTest.setUp(self)
        self.fixtures = FIXTURES
        self.api = BaseApi()

    def mock_requests_post(self, url, data=None):
        ''' Mock method for post method from responses library.
            It replaces the responses.post calls in Api class.
        '''
        url = self.encode_url(url, data)
        if url:
            return self.fixtures[url]

    def test_save_success(self, mock_post):
        mock_post.side_effect = self.mock_requests_post

        response = self.api.post(keyword, params={...})

        # list of asserts

# original class calling requests.post    
import requests
class BaseApi(object):
    def post(self, action, params):
        ''' Sends a POST request to API '''
        response = requests.post(self.build_url(action), data=params).content

The code above fails because the mock method does not provide a mock/stub for ‘content’ attribute present in requests library. Does anyone know how to stub the content attribute?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:04:26+00:00Added an answer on June 11, 2026 at 12:04 pm

    I found the following solution, which only modifies the mock_requests_post method, adding an internal class with the attribute that I need:

    def mock_requests_post(self, url, data=None):
        ''' Mock method for post method from responses library.
            It replaces the responses.post calls in Api class.
        '''
        url = self.encode_url(url, data)
    
        class classWithAttributes(object):
            content = json.dumps(self.fixtures[url])
    
        if url:
            return classWithAttributes()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to document a path from a root object to some attribute
I would like to generate a patch with all commits from a local branch.
We would like to have our TFS automated CI build and test process use
I found the following method in our code base today, and it strikes me
In windows forms, I have some labels in the panel and I would like
I have some collections that I would like to analyze in the Eclipse debugger.
I would like to modify a Spotlight metadata attribute of a file within my
I would like your help in the following : given the .xml file :
My question is very simple, I would like to know if the method CGImageSourceCreateWithData
Thanks for any help in advance. I would like to set id3 tag data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.