If you want to create the control side for the time being, you can postpone the model creation.
test_login.py
import unittest
class TestLoginMethod(unittest.TestCase)
def test_login_returns_true(self):
m = mox.Mox()
user = m.CreateMockAnything() #Intention of User model class to be created in the future
user.account().AndReturn("norobust") #Return the account name for the time being
l = Login() #Login class to be developed
l.user = user #Pass a mock so that it can be referenced from within the Login class
m.ReplayAll() #start mox!
self.assertTrue(l.login()) #Login test!
m.VerifyAll() #mox result check!
VerifyAll with mox to check if the method you set up (user.account () in this case) was called.
When the above test is over and the User class is complete, stop CreateMockAnything and Just make it m.CreateMock (User).
In the above case, it is `l.user = user```, but in reality, write something like
`self.user = get_user_by_id (1) ``` inside Login.login (). I think you will get a user model. If get_user_by_id is really called, you can't pass the User mock you created. In this case, instead of mocking the Login class, you only need to stub get_user_by_id ().
test_login.py
import unittest
class TestLoginMethod(unittest.TestCase)
def test_login_returns_true(self):
m = mox.Mox()
user = m.CreateMockAnything() #User model I haven't made yet ...
user.account().AndReturn("norobust") #Return the account name for the time being
l = Login() #Login class to be developed
m.StubOutWithMock(Login, 'get_user_by_id') #Stub
Login.get_user_by_id().AndReturn(user) #Set the return value of the stubbed function
m.ReplayAll() #start mox!
self.assertTrue(l.login()) #Login test!
m.VerifyAll() #mox result check!
Recommended Posts