2016-06-08 2 views
0

단위 테스트로 on_text 메소드를 다루고 싶습니다. 나는 send_message이 호출되었음을 알기 위해 유니 코드가 아닌 메시지로 on_text을 체크하는 것과 같은 기분이 듭니다.모의 토네이도 클래스의 파이썬

class MyTornadoClass(object): 
    @gen.coroutine 
    def on_text(self, message): 
     """ 
     User message controller 
     """ 
     id_ = message.message_id 
     chat = message.chat 
     text = message.text.strip().replace(" ", "-").replace("[()&?]", "") 
     if not self.is_ascii(text): 
      yield self.send_message(chat.id_, "Sorry, I didn't find anything according to you request. Try again!", 
            reply_to_message_id=id_) 
     else: 
      yield self.perform_search(text, id_, chat) 

내 조롱 테스트는 다음과 같습니다

def test_app(self): 
    message = types.Message({'message_id': '1', 'text': 'Ў', 
          'chat': {"id": 227071993, "first_name": "Sergei", "last_name": "Rudenkov", 
             "type": "private"}, 'from': 343}) 

    zombie = bot_telegram.starter.MyTornadoClass('API_TOKEN', 'SO_WS_URL') 
    zombie.on_text(message) 
    with mock.patch.object(bot_telegram.starter.MyTornadoClass, 'send_message') as mock_zombie: 
     mock_zombie.assert_called_with(227071993, 
             """Sorry, I didn't find anything according to you request. 
             Try again!""", 
             1) 

내가 수신하고 예외 : 내가 뭘 잘못 모른다

Traceback (most recent call last): 
    File "/home/sergei-rudenkov/PycharmProjects/python_tasks/bot_telegram/unit_tests/telezombie_api/starter_test.py", line 21, in test_app 
    1) 
    File "/usr/local/lib/python3.5/dist-packages/mock/mock.py", line 925, in assert_called_with 
    raise AssertionError('Expected call: %s\nNot called' % (expected,)) 
AssertionError: Expected call: send_message(227071993, "Sorry, I didn't find anything according to you request.\n           Try again!", 1) 
Not called 

---------------------------------------------------------------------- 
Ran 1 test in 0.006s 

FAILED (failures=1) 

. 제 실수를 저를 지적하십시오.

+0

무엇인가'is_ascii' : 당신이 내부 .on_text()에 대한 호출 블록을 이동하면 다른 AssertionError를 볼 수 있습니다? – dm03514

+0

'text'가 아스키 문자로 구성되어 있는지 확인하는 방법 –

답변

1

patch.object()을 컨텍스트 관리자로 사용하면 패치는 with 문 다음에 들여 쓰기 된 블록에 적용됩니다.

with mock.patch.object(bot_telegram.starter.MyTornadoClass, 'send_message') as mock_zombie:   
    zombie.on_text(message) 
    mock_zombie.assert_called_with(args) 

AssertionError: Expected call: send_message(227071993, "Sorry, I didn't find anything according to you request.\n           Try again!", 1) 
Actual call: send_message(227071993, "Sorry, I didn't find anything according to you request. Try again!", reply_to_message_id='1') 
관련 문제