public void MockWitMatchingAnyArg() { //Moq MoqRepository .Setup(x => x.GetById(It.IsAny <int>())) .Returns(new Entity(1)); //NSubstitute NsubRepository .GetById(Arg.Any <int>()) .Returns(new Entity(1)); //FakeItEasy A.CallTo(() => FieRepository.GetById(A <int> ._)) .Returns(new Entity(1));; #region Assertion MoqRepository.Object.GetById(0).Should().BeEquivalentTo(new Entity(1)); NsubRepository.GetById(0).Should().BeEquivalentTo(new Entity(1)); FieRepository.GetById(0).Should().BeEquivalentTo(new Entity(1)); #endregion }
public void MockWithMatchingSpecificArg() { //Moq MoqRepository .Setup(x => x.GetById(It.Is <int>(a => a > 0))) .Returns(new Entity(1)); //NSubstitute NsubRepository .GetById(Arg.Is <int>(x => x > 0)) .Returns(new Entity(1)); //FakeItEasy A.CallTo(() => FieRepository.GetById(A <int> .That.IsGreaterThan(0))) .Returns(new Entity(1)); #region Assertion MoqRepository.Object.GetById(1).Should().BeEquivalentTo(new Entity(1)); MoqRepository.Object.GetById(0).Should().Be(null); NsubRepository.GetById(1).Should().BeEquivalentTo(new Entity(1)); NsubRepository.GetById(0).Should().Be(null); FieRepository.GetById(1).Should().BeEquivalentTo(new Entity(1)); FieRepository.GetById(0).Should().BeEquivalentTo(A.Fake <Entity>()); #endregion }
public void MockWitAccessToArg() { //Moq MoqRepository .Setup(x => x.GetById(It.IsAny <int>())) .Returns((int id) => new Entity(id)); //NSubstitute NsubRepository .GetById(Arg.Any <int>()) .Returns(args => new Entity((int)args[0])); //FakeItEasy A.CallTo(() => FieRepository.GetById(A <int> ._)) .ReturnsLazily(x => new Entity(x.Arguments.Get <int>(0))); #region Assertion MoqRepository.Object.GetById(1).Should().BeEquivalentTo(new Entity(1)); NsubRepository.GetById(1).Should().BeEquivalentTo(new Entity(1)); FieRepository.GetById(1).Should().BeEquivalentTo(new Entity(1)); #endregion }
public void MockWitMatchingConcreteArg() { //Moq MoqRepository .Setup(x => x.GetById(1)) .Returns(new Entity(1)); //NSubstitute NsubRepository .GetById(1) .Returns(new Entity(1)); //FakeItEasy A.CallTo(() => FieRepository.GetById(1)) .Returns(new Entity(1)); #region Assetion MoqRepository.Object.GetById(1).Should().BeEquivalentTo(new Entity(1)); MoqRepository.Object.GetById(0).Should().Be(null); NsubRepository.GetById(1).Should().BeEquivalentTo(new Entity(1)); NsubRepository.GetById(0).Should().Be(null); FieRepository.GetById(1).Should().BeEquivalentTo(new Entity(1)); FieRepository.GetById(0).Should().BeEquivalentTo(A.Fake <Entity>()); #endregion }
public void MockMethodWithAccessToRefArg() { //Moq MoqRepository .Setup(x => x.Fill(ref It.Ref <Entity> .IsAny)) .Callback(new FillCallback((ref Entity entity) => entity = new Entity(1))) .Returns(true); //NSubstitute Entity nsubRefEntity = null; NsubRepository .Fill(ref nsubRefEntity) .ReturnsForAnyArgs(x => { x[0] = new Entity(1); return(true); }); //FakeItEasy Entity fieRefEntity = null; A.CallTo(() => FieRepository.Fill(ref fieRefEntity)) .WithAnyArguments() .Returns(true) .AssignsOutAndRefParameters(new Entity(1)); #region Assertion var moqEntity = new Entity(0); MoqRepository.Object.Fill(ref moqEntity).Should().Be(true); moqEntity.Should().BeEquivalentTo(new Entity(1)); var nsubEntity = new Entity(0); NsubRepository.Fill(ref nsubEntity).Should().Be(true); nsubEntity.Should().BeEquivalentTo(new Entity(1)); var fieEntity = new Entity(0); FieRepository.Fill(ref fieEntity).Should().Be(true); fieEntity.Should().BeEquivalentTo(new Entity(1)); #endregion }
public void MockMethodWithAccessToOutArg() { //Moq Entity moqOutEntity; MoqRepository .Setup(x => x.TryGetById(1, out moqOutEntity)) .Callback(new TryGetByIdCallback((int id, out Entity entity) => entity = new Entity(1))) .Returns(true); //NSubstitute Entity nsubOutEntity; NsubRepository .TryGetById(1, out nsubOutEntity) .Returns(x => { x[1] = new Entity(1); return(true); }); //FakeItEasy Entity fieOutEntity; A.CallTo(() => FieRepository.TryGetById(1, out fieOutEntity)) .Returns(true) .AssignsOutAndRefParameters(new Entity(1)); #region Assertion MoqRepository.Object.TryGetById(1, out var moqEntity).Should().Be(true); moqEntity.Should().BeEquivalentTo(new Entity(1)); NsubRepository.TryGetById(1, out var nsubEntity).Should().Be(true); nsubEntity.Should().BeEquivalentTo(new Entity(1)); FieRepository.TryGetById(1, out var fieEntity).Should().Be(true); fieEntity.Should().BeEquivalentTo(new Entity(1)); #endregion }
public void MockWithRefArg() { //Moq var moqRefEntity = new Entity(1); MoqRepository .Setup(x => x.Fill(ref moqRefEntity)) .Returns(true); //NSubstitute var nsubRefEntity = new Entity(1); NsubRepository .Fill(ref nsubRefEntity) .Returns(true); //FakeItEasy var fieRefEntity = new Entity(1); A.CallTo(() => FieRepository.Fill(ref fieRefEntity)) .Returns(true); #region Assertion var moqEntity = moqRefEntity; MoqRepository.Object.Fill(ref moqEntity).Should().Be(true); moqEntity = null; MoqRepository.Object.Fill(ref moqEntity).Should().Be(false); var nsubEntity = nsubRefEntity; NsubRepository.Fill(ref nsubEntity).Should().Be(true); nsubEntity = null; NsubRepository.Fill(ref nsubEntity).Should().Be(false); var fieEntity = fieRefEntity; FieRepository.Fill(ref fieEntity).Should().Be(true); fieEntity = null; FieRepository.Fill(ref fieEntity).Should().Be(false); #endregion }
public void MockMethodWithoutArg() { //Moq MoqRepository.Setup(x => x.GetAll()).Returns(new[] { new Entity(1) }); //NSubstitute NsubRepository.GetAll().Returns(new[] { new Entity(1) }); //FakeItEasy A.CallTo(() => FieRepository.GetAll()).Returns(new[] { new Entity(1) }); #region Assetion MoqRepository.Object.GetAll().Should().BeEquivalentTo(new[] { new Entity(1) }); NsubRepository.GetAll().Should().BeEquivalentTo(new[] { new Entity(1) }); FieRepository.GetAll().Should().BeEquivalentTo(new[] { new Entity(1) }); #endregion }
public void MockWithOutArg() { //Moq var moqOutEntity = new Entity(1); MoqRepository .Setup(x => x.TryGetById(1, out moqOutEntity)) .Returns(true); //NSubstitute Entity nsubOutEntity; NsubRepository .TryGetById(1, out nsubOutEntity) .Returns(x => { x[1] = new Entity(1); return(true); }); //FakeItEasy var fieOutEntity = new Entity(1);; A.CallTo(() => FieRepository.TryGetById(1, out fieOutEntity)) .Returns(true); #region Assertion MoqRepository.Object.TryGetById(1, out var moqEntity).Should().Be(true); moqEntity.Should().BeEquivalentTo(new Entity(1)); NsubRepository.TryGetById(1, out var nsubEntity).Should().Be(true); nsubEntity.Should().BeEquivalentTo(new Entity(1)); FieRepository.TryGetById(1, out var fieEntity).Should().Be(true); fieEntity.Should().BeEquivalentTo(new Entity(1)); #endregion }
public void MockWitMatchingArgAndAnyRefArg() { //Moq MoqRepository .Setup(x => x.Fill(1, ref It.Ref <Entity> .IsAny)) .Callback(new FillCallback((int id, ref Entity entity) => entity = new Entity(1))) .Returns(true); //NSubstitute //Does not have direct support for arg matching ref parameters: //https://github.com/nsubstitute/NSubstitute/issues/401 //FakeItEasy //Does not have direct support for arg matching ref parameters: //https://fakeiteasy.readthedocs.io/en/stable/argument-constraints/ Entity fieRefEntity = null; A.CallTo(() => FieRepository.Fill(1, ref fieRefEntity)) .WhenArgumentsMatch(x => x.Get <int>("id") == 1) .Returns(true) .AssignsOutAndRefParameters(new Entity(1)); #region Assertion var moqEntity = new Entity(0); MoqRepository.Object.Fill(1, ref moqEntity).Should().Be(true); moqEntity.Should().BeEquivalentTo(new Entity(1)); MoqRepository.Object.Fill(0, ref moqEntity).Should().Be(false); moqEntity.Should().BeEquivalentTo(new Entity(1)); var fieEntity = new Entity(0); FieRepository.Fill(1, ref fieEntity).Should().Be(true); fieEntity.Should().BeEquivalentTo(new Entity(1)); FieRepository.Fill(0, ref fieEntity).Should().Be(false); fieEntity.Should().BeEquivalentTo(new Entity(1)); #endregion }
public void MockWitMatchingAnyRefArg() { //Moq MoqRepository .Setup(x => x.Fill(ref It.Ref <Entity> .IsAny)) .Returns(true); //NSubstitute //Does not have direct support for arg matching ref parameters: //https://github.com/nsubstitute/NSubstitute/issues/401 Entity nsubRefEntity = null; NsubRepository .Fill(ref nsubRefEntity) .ReturnsForAnyArgs(true); //FakeItEasy //Does not have direct support for arg matching ref parameters: //https://fakeiteasy.readthedocs.io/en/stable/argument-constraints/ Entity fieRefEntity = null; A.CallTo(() => FieRepository.Fill(ref fieRefEntity)) .WithAnyArguments() .Returns(true); #region Assertion Entity moqEntity = null; MoqRepository.Object.Fill(ref moqEntity).Should().Be(true); Entity nsubEntity = null; NsubRepository.Fill(ref nsubEntity).Should().Be(true); Entity fieEntity = null; FieRepository.Fill(ref fieEntity).Should().Be(true); #endregion }