Guest User

Untitled

a guest
Sep 4th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. Not sure how I Mock this simple Save(User user) method in my .NET code
  2. [TestMethod]
  3. public void GivenANewUserWithValidData_Save_ReturnsTheSameNewUserWithAUserIdDetermined()
  4. {
  5. // Arrange.
  6. var const string passwordSalt = "V4BXAhmHq8IMvR7K20TgoQ=="
  7. var user = new User
  8. {
  9. DisplayName = "Test",
  10. Email = "foo@foo.com",
  11. PasswordSalt = passwordSalt ,
  12. Password = "foobar".ToSha2Hash(passwordSalt)
  13. };
  14.  
  15. var mockUserRepository = new Mock<IRepository<User>>();
  16. mockUserRepository.Setup(x => x.Save(It.IsAny<User>())).Verifiable();
  17. // Configure this repo in our Dependency Injection.
  18. ObjectFactory.Inject(typeof (IRepository<User>), mockUserRepository.Object);
  19.  
  20. // Act.
  21. using (new TransactionScope())
  22. {
  23. UserService.Save(user);
  24. UnitOfWork.Commit(); // <-- not sure about this.. currently it's still
  25. // an EntityFramework context.
  26. // I need to change this to.. something??
  27.  
  28. // Assert.
  29. Assert.IsNotNull(user);
  30. Assert.IsTrue(user.UserId > 0);
  31. }
  32. }
  33.  
  34. public class UserService : IUserService
  35. {
  36. public UserService(IRepository<User> userRepository,
  37. ILoggingService loggingService)
  38. {
  39. // .. snip ..
  40.  
  41. public void Save(User user) { .. }
  42. }
  43. }
  44.  
  45. > 1) How should I test a [UserService.]Save method?
  46. Should I care about the result? and
  47. > 2) If i do care about #1 .. then how do i mock the
  48. result also, so I can test that
  49.  
  50. mockUserRepository.Setup(x => x.Save(user)).Callback(() => user.UserId = 10);
  51. // mocking the value of 10 being insterted into the key
Add Comment
Please, Sign In to add comment