-1
난 의존성을 모의하기 위해 MockingKernel
구성했다. 그럼에도 불구하고 Setup
방법을 사용할 수 없습니다.를 셋업 조롱 주입 객체
아이디어가 있으십니까?
설명서를 읽었습니다. 나는 몇 가지 예를 묻고있다. 그럼에도 불구하고, 나는 설명서가 약간 가난하다고 생각한다.
나는이 시도했습니다
편집 :
public void Test() {
Core.Configuration.UserIdentity userConfiguration = Core.Configuration.UserIdentity.Create("u1", "p1");
IEnumerable<Core.Configuration.UserIdentity> configurationUsers = new List<Core.Configuration.UserIdentity>() { userConfiguration };
this.IoCKernel.Get<Core.Configuration.ICoreConfiguration>().UserIdentities.Returns(configurationUsers);
//Testing
Core.Kernel kernel = this.IoCKernel.Get<Core.Kernel>();
kernel.Received(1).AddUser(Arg.Any<Core.Identity.UserIdentity>());
}
그럼에도 불구하고, 내가 마지막 줄에 지금이 NSubstitute.Exceptions.NotASubstituteException
예외 메시지를 받고 있어요 : 같은
NSubstitute 확장 방법. Received()는 Substitute.For() 및 관련 메서드를 사용하여 만든 객체에서만 호출 할 수 있습니다.
제가 알기로, 적어도 한 번만 AddUser
메서드를 호출하려고합니다. 내 Core.Kernel
구현에 따라 AddUser
을 호출해야합니다. 다음 패키지를 사용
using Ninject;
using Ninject.MockingKernel.NSubstitute;
using NSubstitute;
using NUnit.Framework;
namespace ClassLibrary1
{
[TestFixture]
public class MyTests
{
[Test]
public void Test1()
{
using (var kernel = new NSubstituteMockingKernel())
{
var substitute = kernel.Get<IDummyService>();
substitute.ReturnInt().Returns(1);
var instance = kernel.Get<DummyClass>();
Assert.AreEqual(1, instance.Calc());
substitute.Received(1).ReturnInt();
}
}
public interface IDummyService
{
int ReturnInt();
}
public class DummyClass
{
private IDummyService _dummyService;
public DummyClass(IDummyService dummyService)
{
_dummyService = dummyService;
}
public int Calc()
{
return _dummyService.ReturnInt();
}
}
}
}
:
감사 알렉산드르. 나는 지금 포스트에 추가 된 코드를 시도했다. 그럼에도 불구하고 예외 메시지가 나타납니다 ... – Jordi
@ Jordi 어떤 예외가 있습니까? 자세한 내용을 알려주십시오. –
예외 유형은 NSubstitute.Exceptions.NotASubstituteException입니다 ... – Jordi