[英]How to mock this unit test in Python?
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.
在此测试方法(test_get_all_products)中,我希望传递产品列表和针对dal标识的DB调用的模拟响应。
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. 但是,当我断言来自模拟对象的product [0] ['termMonths'] == 1时,它会失败,因为termMonths实际上是Mock对象本身,而不是我期望的返回值(product_sku_cost)。
Please help me figure out what I am doing incorrectly above. 请帮助我找出上面我做错的事情。
It is just a TYPO error: try to change SKUCost
to skucost
where you configured dal
. 只是TYPO错误:尝试将
SKUCost
更改为在dal
处配置的skucost
。 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. 无论如何,您的测试中还有其他问题:您不需要在测试中打补丁,因为您正在传递
dal
对象,而没有使用model.dal
引用。 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. 我不知道是什么
model.dal
但是如果您想要做的是具有相同的签名/属性 ,则可以使用create_autospec
进行构建。
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. 如果
model.dal
是一个类,则在创建自动指定时使用instance=True
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.