// Declared in the module. constexpr int kThumbnailSizes[] = {480, 576, 720}; // Returns thumbnails of various sizes for the given image. std::vector<Image> GetThumbnails(const Image& image) { std::vector<Image> thumbnails; for (const int size : kThumbnailSizes) { thumbnails.push_back(ResizeImage(image, size)); } return thumbnails; }
std::vector<Image> GetThumbnails(const Image& image, absl::Span<const int> sizes) { std::vector<Image> thumbnails; for (const int size : sizes) { thumbnails.push_back(ResizeImage(image, size)); } return thumbnails; }
// Declared in the public header. inline constexpr int kDefaultThumbnailSizes[] = {480, 576, 720}; // Default argument allows the function to be used without specifying a size. std::vector<Image> GetThumbnails(const Image& image, absl::Span<const int> sizes = kDefaultThumbnailSizes);
// Mock a salary payment library @Mock SalaryProcessor mockSalaryProcessor; @Mock TransactionStrategy mockTransactionStrategy; ... when(mockSalaryProcessor.addStrategy()).thenReturn(mockTransactionStrategy); when(mockSalaryProcessor.paySalary()).thenReturn(TransactionStrategy.SUCCESS); MyPaymentService myPaymentService = new MyPaymentService(mockSalaryProcessor); assertThat(myPaymentService.sendPayment()).isEqualTo(PaymentStatus.SUCCESS);
FakeSalaryProcessor fakeProcessor = new FakeSalaryProcessor(); // Designed for tests MyPaymentService myPaymentService = new MyPaymentService(fakeProcessor); assertThat(myPaymentService.sendPayment()).isEqualTo(PaymentStatus.SUCCESS);
@Mock MySalaryProcessor mockMySalaryProcessor; // Wraps the SalaryProcessor library ... // Mock the wrapper class rather than the library itself when(mockMySalaryProcessor.sendSalary()).thenReturn(PaymentStatus.SUCCESS); MyPaymentService myPaymentService = new MyPaymentService(mockMySalaryProcessor); assertThat(myPaymentService.sendPayment()).isEqualTo(PaymentStatus.SUCCESS);
def setUp(self): self.users = [User('alice'), User('bob')] # This field can be reused across tests. self.forum = Forum() def testCanRegisterMultipleUsers(self): self._RegisterAllUsers() for user in self.users: # Use a for-loop to verify that all users are registered. self.assertTrue(self.forum.HasRegisteredUser(user)) def _RegisterAllUsers(self): # This method can be reused across tests. for user in self.users: self.forum.Register(user)
def setUp(self): self.forum = Forum() def testCanRegisterMultipleUsers(self): # Create the users in the test instead of relying on users created in setUp. user1 = User('alice') user2 = User('bob') # Register the users in the test instead of in a helper method, and don't use a for-loop. self.forum.Register(user1) self.forum.Register(user2) # Assert each user individually instead of using a for-loop. self.assertTrue(self.forum.HasRegisteredUser(user1)) self.assertTrue(self.forum.HasRegisteredUser(user2))