0

動的に作成された関数の戻り値を使用するコードをテストしています。テストしているコードが、スプーフィングされたデータを使用して「email_invoice」という関数を正しく呼び出すことを確認する必要があります...

動的に作成された関数がリモート システムにヒットするため、呼び出しの結果を偽装しています。

class MyTest(unittest2.Test):

    def setUp(self):

        patcher = mock.patch('soc.product.views.API')
        patch = patcher.start()

        self.order_id = 'fake_order_id'

        # The `API` class has methods that are dynamically created.
        # The method `API.CreateOrder` needs to be patched to return `self.order_id`
        # When testing that a resulting method is called, I get a failed assertion:

        #AssertionError: Expected call: email_invoice(<User: User(id=46, merchant_id=503579)>, 'fake123123123')
        #Actual call: email_invoice(<User: User(id=46, merchant_id=503575)>, <MagicMock name='API().CreateOrder().OrderID' id='140700602174736'>)

        # soc.product.views.API.CreateOrder => self.order_id
        CreateOrderResult = mock.NonCallableMock()
        CreateOrderResult.OrderId = self.order_id
        patch.CreateOrder = mock.Mock()
        patch.CreateOrder.return_value = CreateOrderResult


    def test_that_stuff_out_homie(self):

         ... doing stuff ...

         user = ... a result of doing stuff ...

         self.patches['email_invoice'].assert_called_once_with(user, self.order_id)

言及されているように、アサーションは次のように失敗します。

AssertionError: Expected call: email_invoice(<User: User(id=46, merchant_id=503579)>, 'fake123123123')
Actual call: email_invoice(<User: User(id=46, merchant_id=503575)>, <MagicMock name='API().CreateOrder().OrderID' id='140700602174736'>)

では、これをテストする適切な/正しい方法は何ですか?

4

1 に答える 1