How to mock this unit test in Python?

DLS

This is the method I want to test. In this test method (test_get_all_products), I want to pass in a list of products and a mocked response for the DB call which is identified by dal.

def get_all_user_standalone_products(all_products, dal):
standalone_products = []
if all_products is not None:
    all_products = all_products['userRenewableProduct']
    for product in all_products:
        sku_id = product.get('skuID', 0)
        sku_cost_id = product.get('skuCostID', 0)
        standalone_product = which_standalone_product(sku_id)

        if product.get('isDisabled') or standalone_product is None:
            continue

        product['productType'] = standalone_product['name']

        sku_cost_data = dal.skucosts.get_costs_for_sku_cost_id(
            sku_cost_id)
        product['termMonths'] = sku_cost_data['termMonths']

        upgrade_sku_ids = standalone_product.get(
            'upgrade_sku_ids', [])
        if len(upgrade_sku_ids) > 0:
            product['canUpgrade'] = True

        product['upgradeSkus'] = upgrade_sku_ids
        standalone_products.append(product)
return standalone_products

This is my test

product_sku_cost= {
        u'testPriceSetID':u'',
        u'skuID':88,
        u'currencyTypeID':1,
        u'termMonths':1,
        u'dateCreated':   u'2015-10-07T17:03:00   Z',
        u'skuCostID':2840,
        u'cost':9900,
        u'skuTypeID':13,
        u'dateModified':   u'2015-10-07T17:03:00   Z',
        u'isDefault':True,
        u'name':u'Product'}

@patch('model.dal')
def test_get_all_products(self, dal):
      # this is my mock - I want it to return the dictionary above.
      dal.SKUCosts.get_costs_for_sku_cost_id.return_value = product_sku_cost
      products = get_all_user_standalone_products(renewable_products, dal)

      assert products[0]['canUpgrade'] is True
      assert products[0]['termMonths'] == 1

But when I assert the products[0]['termMonths'] == 1 which comes from the mocked object, it fails because the termMonths is actually the Mock object itself, not the return value(product_sku_cost) I was expecting.

Please help me figure out what I am doing incorrectly above.

Michele d'Amico

It is just a TYPO error: try to change SKUCost to skucost where you configured dal. So configuration line become:

dal.skucsts.get_costs_for_sku_cost_id.return_value = product_sku_cost

Anyway there is some other issues in your test: You don't need to patch anything in your test because you're passing dal object and nothing use model.dal reference. I don't know what model.dal but if what you want to do is to have the same signature/properties you can use create_autospec to build it.

Your test can be:

def test_get_all_products(self, dal):
   # this is my mock - I want it to return the dictionary above.
   dal = create_autospec(model.dal)
   dal.skucosts.get_costs_for_sku_cost_id.return_value = product_sku_cost
   ...

If model.dal is a class use instance=True when you create autospec.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to mock springSecurityService in an unit test

From Dev

How do I mock a class in a Python unit test?

From Dev

How to supply a mock class method for python unit test?

From Dev

How to properly mock big XML in unit test in Django and Python?

From Dev

How to android unit test and mock a static method

From Dev

how to mock $window to unit test AngularJS service?

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

How to mock the UserAgent property for an HttpRequest in a unit test?

From Dev

How to unit test this function without mock asserts?

From Dev

How to mock browserHistory in unit test environment?

From Dev

How to mock multiple components in camel unit test?

From Dev

How to mock an Akka Actor to Unit Test a class?

From Dev

How to mock a generic parameter for a unit test in Java?

From Dev

How to mock a FormBuilder for Angular Component Unit Test

From Dev

How to mock multiple components in camel unit test?

From Dev

How to mock the UserAgent property for an HttpRequest in a unit test?

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

How to define Mock Cursor for Android Unit Test

From Dev

Mock IMemoryCache in unit test

From Dev

How to use python Mock side_effect to act as a Class method in unit test

From Dev

How to use python Mock side_effect to act as a Class method in unit test

From Dev

Python unit test mock, get mocked function's input arguments

From Dev

Better way to mock class attribute in python unit test

From Dev

how to unit test a python process

From Java

How to mock JWT authenticaiton in a Spring Boot Unit Test?

From Dev

How to unit test async Redux actions to mock ajax response

From Dev

How do I mock the result of a promise in an AngularJS unit test?

From Dev

How to mock SoapException using Moq to unit test error handling

From Dev

In Ember js, how to create or mock hasMany relationship in unit test

Related Related

  1. 1

    How to mock springSecurityService in an unit test

  2. 2

    How do I mock a class in a Python unit test?

  3. 3

    How to supply a mock class method for python unit test?

  4. 4

    How to properly mock big XML in unit test in Django and Python?

  5. 5

    How to android unit test and mock a static method

  6. 6

    how to mock $window to unit test AngularJS service?

  7. 7

    How to mock AngularFire 2 service in unit test?

  8. 8

    How to mock the UserAgent property for an HttpRequest in a unit test?

  9. 9

    How to unit test this function without mock asserts?

  10. 10

    How to mock browserHistory in unit test environment?

  11. 11

    How to mock multiple components in camel unit test?

  12. 12

    How to mock an Akka Actor to Unit Test a class?

  13. 13

    How to mock a generic parameter for a unit test in Java?

  14. 14

    How to mock a FormBuilder for Angular Component Unit Test

  15. 15

    How to mock multiple components in camel unit test?

  16. 16

    How to mock the UserAgent property for an HttpRequest in a unit test?

  17. 17

    How to mock AngularFire 2 service in unit test?

  18. 18

    How to define Mock Cursor for Android Unit Test

  19. 19

    Mock IMemoryCache in unit test

  20. 20

    How to use python Mock side_effect to act as a Class method in unit test

  21. 21

    How to use python Mock side_effect to act as a Class method in unit test

  22. 22

    Python unit test mock, get mocked function's input arguments

  23. 23

    Better way to mock class attribute in python unit test

  24. 24

    how to unit test a python process

  25. 25

    How to mock JWT authenticaiton in a Spring Boot Unit Test?

  26. 26

    How to unit test async Redux actions to mock ajax response

  27. 27

    How do I mock the result of a promise in an AngularJS unit test?

  28. 28

    How to mock SoapException using Moq to unit test error handling

  29. 29

    In Ember js, how to create or mock hasMany relationship in unit test

HotTag

Archive