Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Not sure how I Mock this simple Save(User user) method in my .NET code
- [TestMethod]
- public void GivenANewUserWithValidData_Save_ReturnsTheSameNewUserWithAUserIdDetermined()
- {
- // Arrange.
- var const string passwordSalt = "V4BXAhmHq8IMvR7K20TgoQ=="
- var user = new User
- {
- DisplayName = "Test",
- Email = "foo@foo.com",
- PasswordSalt = passwordSalt ,
- Password = "foobar".ToSha2Hash(passwordSalt)
- };
- var mockUserRepository = new Mock<IRepository<User>>();
- mockUserRepository.Setup(x => x.Save(It.IsAny<User>())).Verifiable();
- // Configure this repo in our Dependency Injection.
- ObjectFactory.Inject(typeof (IRepository<User>), mockUserRepository.Object);
- // Act.
- using (new TransactionScope())
- {
- UserService.Save(user);
- UnitOfWork.Commit(); // <-- not sure about this.. currently it's still
- // an EntityFramework context.
- // I need to change this to.. something??
- // Assert.
- Assert.IsNotNull(user);
- Assert.IsTrue(user.UserId > 0);
- }
- }
- public class UserService : IUserService
- {
- public UserService(IRepository<User> userRepository,
- ILoggingService loggingService)
- {
- // .. snip ..
- public void Save(User user) { .. }
- }
- }
- > 1) How should I test a [UserService.]Save method?
- Should I care about the result? and
- > 2) If i do care about #1 .. then how do i mock the
- result also, so I can test that
- mockUserRepository.Setup(x => x.Save(user)).Callback(() => user.UserId = 10);
- // mocking the value of 10 being insterted into the key
Add Comment
Please, Sign In to add comment