2017-09-26 3 views
0

私は信号をテストしようとします。私はapp.signals.pypythonはテストで既存の引数を求めています

from django.dispatch import Signal 
task_completed = Signal(providing_args=['balance']) 
における信号のアイテムを持っている

funcがuser.viewsであり、それは、タスクに応じてバランスを更新FUNCは、タスクの作成後task.modelsで呼び出され

from django.contrib.messages import success 
from users.models import User 
from django.db.models import F 
from freelance.signals import task_completed 


def update_balance(cls, balance): 
    User.objects.select_for_update().filter(user_type=User.CUSTOMER).update(
     balance=F('balance') - balance 
    ) 
    User.objects.select_for_update().filter(user_type=User.EXECUTER).update(
     balance=F('balance') + balance 
    ) 
    if success: 
     task_completed.send_robust(
      sender=cls, 
      balance=balance, 
     ) 

コスト

@receiver(post_save, sender=Task) 
def task_post_save(sender, instance, **kwargs): 
    instance.assignee.update_balance(instance.money) 

そして最後には、私はこのすべてのものを作りたいということをテスト

class TestCharge(TestCase): 
    def test_should_send_signal_when_charge_succeeds(self): 
     self.signal_was_called = False 
     self.total = None 

     def handler(sender, balance, **kwargs): 
      self.signal_was_called = True 
      self.total = balance 

     task_completed.connect(handler) 

     update_balance(100) 

     self.assertTrue(self.signal_was_called) 
     self.assertEqual(self.total, 100) 

     task_completed.disconnect(handler) 

しかし、あなたは、あなたがupdate_balance()内の必要な引数を渡していない、エラーから見ることができるように、それは

TypeError: update_balance() missing 1 required positional argument: 'balance' 

答えて

1

のような間違いを提供します。 エラーは、この行である:

update_balance(100) 

しかし、あなたの関数の定義に従って:あなたはこのようなあなたのコードを更新する必要があるので、二つの引数を取る

def update_balance(cls, balance): 

update_balance:

class TestCharge(TestCase): 
    def test_should_send_signal_when_charge_succeeds(self): 
     self.signal_was_called = False 
     self.total = None 

     def handler(sender, balance, **kwargs): 
      self.signal_was_called = True 
      self.total = balance 

     task_completed.connect(handler) 

     update_balance("First value here",100) 

     self.assertTrue(self.signal_was_called) 
     self.assertEqual(self.total, 100) 

     task_completed.disconnect(handler) 
関連する問題