Опитвам се да създам прост модулен тест с помощта на рамката Ninject Moq и по някаква причина не мога да накарам метода за настройка да работи правилно. Доколкото разбирам, методът за настройка по-долу трябва да инжектира хранилището в класа на услугата с предварително дефинирания резултат true.
[TestFixture]
public class ProfileService : ServiceTest
{
private readonly Mock<IRepository<Profile>> _profileRepoMock;
public ProfileService()
{
MockingKernel.Bind<IProfileService>().To<Data.Services.Profiles.ProfileService>();
_profileRepoMock = MockingKernel.GetMock<IRepository<Profile>>();
}
[Test]
public void CreateProfile()
{
var profile = new Profile()
{
Domain = "www.tog.us.com",
ProfileName = "Tog",
};
_profileRepoMock.Setup(x => x.SaveOrUpdate(profile)).Returns(true);
var profileService = MockingKernel.Get<IProfileService>();
bool verify = profileService.CreateProfile(Profile);
_profileRepoMock.Verify(repository => repository.SaveOrUpdate(profile), Times.AtLeastOnce());
Assert.AreEqual(true, verify);
}
}
Когато се опитам да го проверя, получавам тази грешка:
Очаква се извикване на макета поне веднъж, но никога не е извършено: repository => repository.SaveOrUpdate(.profile)
Конфигурирани настройки: x => x.SaveOrUpdate(.profile), Times.Never
Извършени извиквания: IRepository`1.SaveOrUpdate(DynamicCms.Data.DataModels.Profile)
Ето метода CreateProfile в класа ProfileService:
public class ProfileService : IProfileService
{
private readonly IRepository<Profile> _profileRepo;
public ProfileService(IRepository<Profile> profileRepo)
{
_profileRepo = profileRepo;
}
public bool CreateProfile(ProfileViewModel profile)
{
Profile profileToCreate = new Profile
{
Domain = profile.Domain,
ProfileName = profile.Name
};
bool verify = _profileRepo.SaveOrUpdate(profileToCreate);
if (verify)
{
return true;
}
return false;
}
}
РЕДАКТИРАНЕ: Замених обекта Profile, в който се предава
_profileRepoMock.Setup(x => x.SaveOrUpdate(profile)).Returns(true);
с
_profileRepoMock.Setup(x => x.SaveOrUpdate(It.IsAny<Profile>())).Returns(true);
Този метод работи сега, но защо точно не работи по-рано, когато подадох един и същ обект и в методите Verify и Setup.
Поглеждайки назад към него, тъй като този метод е настроен да връща конкретна стойност, няма особено значение какво се предава в него, но би било хубаво да се знае.
ProfileService.CreateProfile
метод? - person nemesv   schedule 02.06.2013