English 中文(简体)
如何在沙捞越一个等级的恢复价值?
原标题:How to patch the return value of initializing a class in Python?

我试图将初始物体的价值压在单位试验的先发价之上。 长期以来,我使用模拟图书馆,经过测试。

这里是表明问题的法典:

def function_to_test():
    new_object = Object(arg1, arg2, arg3)

    return func1(new_object)

为履行这一职能,我要测试该物体的初始化:arg1 arg2arg3,但我还要将new_object的数值上调,因此,我可以测试

My current test is something like:

def test_function_to_test(mocker):
    mocker.patch(Object.__init__, return_value=None)
    mocker.patch(func1)

    function_to_test()
    
    Object.__init__.assert_called_once_with(arg1, arg2, arg3)
    
    mock_object = "test"

    func1.assert_called_once_with(mock_object) # this line fails

要想通过最后一行,就必须将新目标的价值提升到先令价值。 我尝试修改<代码>return_ Value,以配送初始器,但由于_init_<>>><>>> /code功能不退回任何东西,我发现了一个错误。 是否有办法使“客观返回”成为预先界定的价值?

问题回答

Don t 设定了收益价值,只是让它把另一辆 mo车作为回报价值。 然后,你还可以使用,在你后来的断言中,这种回报价值相同。

它将研究这样的情况:

from mymod import function_to_test

def test_function_to_test(mocker):
    mock_Object = mocker.patch("mymod.Object")
    mock_func1 = mocker.patch("mymod.func1")
    function_to_test()
    mock_Object.assert_called_once_with(arg1, arg2, arg3)
    mock_func1.assert_called_once_with(mock_Object.return_value)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...