Nunit Moq单元测试设置不工作

Nunit Moq单元测试设置不工作,nunit,ninject,moq,Nunit,Ninject,Moq,我正在尝试使用Ninject Moq框架创建一个简单的单元测试,由于某些原因,我无法使设置方法正常工作。据我所知,下面的设置方法应该使用预定义的true结果将存储库注入到服务类中 [TestFixture] public class ProfileService : ServiceTest { private readonly Mock<IRepository<Profile>> _profileRepoMock; public ProfileServ

我正在尝试使用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);
    }
}

_profileRepoMock.Setup(x => x.SaveOrUpdate(It.IsAny<Profile>())).Returns(true); 
\u profileRepoMock.Setup(x=>x.SaveOrUpdate(It.IsAny())。返回(true);
这个方法现在可以工作了,但为什么在我将同一个对象同时传递到Verify和Setup方法时它不能工作呢


回过头来看,由于此方法设置为返回特定值,因此传递给它的内容实际上并不重要,但最好知道。

您不需要使用NInject进行单元测试:

[TestFixture]
public class ProfileServiceTest : ServiceTest
{
    private readonly Mock<IRepository<Profile>> _profileRepoMock;
}
[SetUp]
public void Setup()
{
    _profileRepoMock = new Mock<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 = new ProfileService(_profileRepoMock.Object);
    bool verify = profileService.CreateProfile(Profile);

    _profileRepoMock.Verify(repository => repository.SaveOrUpdate(profile), Times.AtLeastOnce());

    Assert.AreEqual(true, verify);
}
[TestFixture]
公共类ProfileServiceTest:ServiceTest
{
私有只读Mock _profileRepoMock;
}
[设置]
公共作废设置()
{
_profileRepoMock=newmock();
}
[测试]
public void CreateProfile()
{
var profile=new profile()
{
Domain=“www.tog.us.com”,
ProfileName=“Tog”,
};
_profileRepoMock.Setup(x=>x.SaveOrUpdate(profile))。返回(true);
var profileService=新的profileService(_profileRepoMock.Object);
bool verify=profileService.CreateProfile(Profile);
_profileRepoMock.Verify(repository=>repository.SaveOrUpdate(profile),Times.atlestOnce());
Assert.AreEqual(true,verify);
}
}

编辑:这个类需要稍微修改一下

public class ProfileService : IProfileService
{
    private readonly IRepository<Profile> _profileRepo;

    public ProfileService(IRepository<Profile> profileRepo)
    {
        _profileRepo = profileRepo;
    }
        public bool CreateProfile(ProfileViewModel profile)
    {

        bool verify = _profileRepo.SaveOrUpdate(profile);

        if (verify)
        {
            return true;
        }

        return false;
    }
 }
public类ProfileService:IProfileService
{
私人只读电子存储_profileRepo;
公共档案服务(IRepository profileRepo)
{
_profileRepo=profileRepo;
}
public bool CreateProfile(ProfileViewModel配置文件)
{
bool verify=_profileRepo.SaveOrUpdate(配置文件);
如果(验证)
{
返回true;
}
返回false;
}
}

你能发布你的
ProfileService.CreateProfile
方法吗?设置好了,我刚刚添加了该方法。谢谢你的建议,但实际上我还是收到了与此方法相同的错误消息。这是因为传递给CreateProfile的配置文件对象与传递给SaveOrUpdate的配置文件对象不同。它们都是同一类型的,所以使用它。当然,IsAny也可以。我已经更新了我的回复,以反映适当的解决方案。
[TestFixture]
public class ProfileServiceTest : ServiceTest
{
    private readonly Mock<IRepository<Profile>> _profileRepoMock;
}
[SetUp]
public void Setup()
{
    _profileRepoMock = new Mock<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 = new ProfileService(_profileRepoMock.Object);
    bool verify = profileService.CreateProfile(Profile);

    _profileRepoMock.Verify(repository => repository.SaveOrUpdate(profile), Times.AtLeastOnce());

    Assert.AreEqual(true, verify);
}
public class ProfileService : IProfileService
{
    private readonly IRepository<Profile> _profileRepo;

    public ProfileService(IRepository<Profile> profileRepo)
    {
        _profileRepo = profileRepo;
    }
        public bool CreateProfile(ProfileViewModel profile)
    {

        bool verify = _profileRepo.SaveOrUpdate(profile);

        if (verify)
        {
            return true;
        }

        return false;
    }
 }