User-1410221107 posted
Hi,
I am trying to mock a method having ref parameters and it should return 1 but it always return 1 .I am not sure what i am doing wrong below is the sample i have created based
on my problem. Any help would be greatly appreciated.
In the below code following method always return 0 even though i mocked it to 1
repo.Setup(c => c.SaveCustomerContact(ref dStamp)).Returns(1);
Below is complete sample code I have created.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Moq;
using NUnit.Framework;
namespace Westpac.Modules.UnitTests
{
[TestFixture]
class TestClass
{
Mock<ILegacy> legacyStub = new Mock<ILegacy>();
Mock<Isecond> second = new Mock<Isecond>();
[SetUp]
public void setup()
{
long bar = 0;
legacyStub.Setup(l => l.Foo(ref bar))
.Returns(bar);
}
public void SetupFixture()
{
}
[Test]
public void Test1()
{
long no = 123;
long dStamp = 1002;
var repo = new Mock<IRemotingHandler>(MockBehavior.Strict);
var secondrepo = new Mock<Isecond>();
var handler = new StubRemotingHandler(secondrepo.Object);
repo.Setup(c => c.SaveCustomerContact(ref dStamp)).Returns(1);
var res = handler.SaveCustomerContact(ref no);
Assert.AreEqual(123, res);
}
}
public interface Isecond
{
long secondMethod(long no);
}
public interface IRemotingHandler
{
long SaveCustomerContact(ref long contactno);
}
public class StubRemotingHandler : IRemotingHandler
{
public long savedContact;
Isecond _second;
public StubRemotingHandler(Isecond second)
{
_second = second;
}
public long SaveCustomerContact(ref long contactno)
{
savedContact = contactno;
return _second.secondMethod(savedContact);
}
}
public class SecondClass : Isecond
{
public long secondMethod(long no)
{
int number = 0;
return number;
}
}
public interface ILegacy
{
long Foo(ref long bar);
}
}
Please let me know if you need any further information