简体   繁体   English

function里面的object的模拟方法[python]

[英]Mock method of an object inside function [python]

I have two files:- file1.py and file2.py我有两个文件:- file1.py 和 file2.py

file2.py has following code:- file2.py 具有以下代码:-

import json

def fun1():
    s = "{'function1': 'val1'}"
    s = json.dumps(s)
    print("in fun1 ", s)
    return s

def fun2():
    s = "{'function2': 'value2'}"
    s = json.dumps(s)
    print("in fun2 ", s)
    return s

def fun5():
    fun2()
    return fun1()

file1.py has following code file1.py 有以下代码

from mockito import when, unstub
from file2 import fun5

def mock_the_function():
    when("file2.fun1.json").dumps(...).thenReturn("something something")
    print(fun5())
    unstub()

I want to mock "dumps" inside "fun1" only, not "fun2".我只想在“fun1”中模拟“转储”,而不是“fun2”。 Code which I have written is showing error.我写的代码显示错误。 I don't want to do it by parameter comparison.我不想通过参数比较来做到这一点。 Is there any other way I can have a function being passed inside "when"?有没有其他方法可以让 function 在“何时”内传递?

First a quick note:首先是一个快速说明:

  • json.dumps takes an object, not a string, so it's kind of redundant to to call it as you are inside fun1 and fun2 . json.dumps采用 object,而不是字符串,因此在fun1fun2中调用它有点多余。 Perhaps you're looking for json.loads ?也许您正在寻找json.loads

Next I'd consider some different approaches:接下来我会考虑一些不同的方法:

  • When you mock json.dumps , you want to mock the the json property of the file2 module, so it would just be when("file2.json")... in the setup code for your test.当你模拟json.dumps时,你想模拟file2模块的json属性,所以它只是when("file2.json")...在你的测试设置代码中。

  • Because (as above), we're mocking the json module globally in the file2 module, it's not possible to, as you asked, in that context of a single test, mock json.dumps inside of fun1 but not fun2 , but what I'd suggest is to simply have two tests, and unstub in a tear down method.因为(如上所述),我们是 mocking json模块全局在file2模块中,正如您所要求的那样,在单个测试的上下文中,模拟json.dumpsfun1内部而不是fun2是不可能的,但是我建议是简单地进行两个测试,并在拆卸方法中unstub For example:例如:

from unittest import TestCase

class TestExample(TestCase):
    def tearDown(self):
        unstub()

    def test_fun1(self):
        when("file2.json").dumps(...).thenReturn("something something")
        # in this invocation json is stubbed
        self.assertEqual(fun1(), "something something")

    def test_fun2(self):
        # but now unstub has been called so it won't be stubbed anymore.
        self.assertEqual(fun2(), "...output of fun2...")

Another alternative is for fun1 and fun2 to take a function that will do the work that the global json module is currently doing.另一种选择是fun1fun2使用 function 来完成全局json模块当前正在做的工作。 This use of the Dependency Inversion Principle makes the code more testable, and means you don't even need mockito in your tests.这种依赖倒置原则的使用使代码更易于测试,并且意味着您在测试中甚至不需要mockito For example:例如:

def fun1(json_decoder):
   s = "..."
   return json_decoder(s)

# ....
from unittest.mock import MagicMock

def test_fun_1():
   mock_decoder = MagicMock()
   mock_decoder.return_value = "asdf"
   assert fun1(mock_decoder) == "asdf"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM