I was trying to Mock some some functionality of my Services. The code goes like this.
public interface IObj {
public bool anotherMethod(string input, out string responseString);
}
public class SomeClass {
public bool SomeMethod(string input, out IObj outputObj) {
// some logic
if (logic is correct) {
outputObj = // object of IObj
return true;
}
outputObj = null;
return false;
}
}
public class Service {
public void executingMethod(){
if (this.someClassObj.SomeMethod(this.inputString, out outputObj) {
if (outputObj.anotherMethod(this.anotherInputString, out responseString)
{
// some business logic
}
}
}
}
Now i want to Mock the method executingMethod behaviour vai UnitTest using Moq and xUnit.
But while mocking I am getting issue with out parameter. In this way I am trying to mock the behaviour.
[Fact]
public void MockingMethod(){
// Arrange
Mock<SomeClass> mockSomeClass = new Mock<SomeClass>();
Mock<IObj> mockIObj = new Mock<IObj>();
string mockedResponse = "someResponse";
// here i am getting the issue, as out is expecting actual object not mocked object.
mockSomeClass.Setup(s => s.SomeMethod(It.IsAny<string>(), out mockIObj).Returns(true);
mockIObj.Setup(s => s.anotherMethod(It.IsAny<string(), out mockedResponse).Returns(true);
}
Any help will be much appreciated. TIA.
I tried to use as suggested by #Roman as well.
mockSomeClass.Setup(s => s.SomeMethod(It.IsAny<string>(), out mockIObj.Object).Returns(true);
but it is throwing this error -> "A property or indexer may not be passed as out or ref parameter"
Try “mockIObj.Object” . This should pass in the mocked object rather than the mocked instance
I Found other way to resolve this issue.
This method public bool SomeMethod(string input, out IObj outputObj)
was calling one another method like this public bool oneAnotherMethod(string input, out string outputResponse) mocking this method resolve the issue.
I was not knowing that mocking internal calls can propagate above the calling chain.
Thanks for all the help.
Related
I am writing tests for our C# 7 application and struggle with mocking an Extension method. I am using TypeMock 8.6.10.3.
Here is a simplified example:
internal class InnerClass
{
private readonly string _description;
public InnerClass(string description)
{
_description = description;
}
public string GetDescription()
{
return _description;
}
}
internal class OuterClass
{
public void DoSthWithExtension(int someNumber)
{
var innerClass = new InnerClass("InnerClassDescription");
innerClass.Extension(someNumber);
}
}
internal static class Extensions
{
public static void Extension(this InnerClass innerClass, int someNumber)
{
var d = innerClass.GetDescription();
}
}
public void TestExtension()
{
// I want to fake the method "InnerClass.Extension()"
// which is called by "OuterClass.DoSthWithExtension()".
// I don't have access to the InnerClass instance though.
// So unfortunately I have to fake them all.
var fakedInnerClasses = Isolate.Fake.AllInstances<InnerClass>();
Isolate.WhenCalled(() => Extensions.Extension(fakedInnerClasses, 11)).WithExactArguments().DoInstead(
c =>
{
// The test doesn't go in here. The second parameter is correct,
// the first one obviously not. But what is expected as a first parameter then?
var oc2 = new OuterClass();
// Here I call InnerClass.Extension() again.
// The test should now go into the faked method underneath.
oc2.DoSthWithExtension(22);
});
Isolate.WhenCalled(() => Extensions.Extension(fakedInnerClasses, 22)).WithExactArguments().DoInstead(
c =>
{
// As above, the test code doesn't go in here.
});
// In here an instance of InnerClass is created and
// InnerClass.Extension(11) is called.
var oc1 = new OuterClass();
oc1.DoSthWithExtension(11);
}
As the this parameter of the Extension method I choose the faked instances of InnerClass. Thats what I assume is needed. But TypeMock does not bring me into the faked method. Obviously its the wrong parameter. But which one should I choose then?
Based on comments and updated question, the part that is confusing is why the other outer class is needed. The shown inner class has no dependency on the outer. Why would the mock then need to create a new outer class?
That aside, based on the docs its appears that you need to setup the extension class so that you can fake the static extension calls.
Isolate.Fake.StaticMethods(typeof(Extensions));
//...
Original answer
Do not fake the extension method in this case. You know what the extension method calls. so fake that.
public void TestExtension() {
//Arrange
string expected = "Fake result";
var fakedInnerClasses = Isolate.Fake.AllInstances<InnerClass>();
Isolate.WhenCalled(() => fakedInnerClasses.GetDescription())
.WillReturn(expected);
var subject = new OuterClass();
//Act
subject.DoSthWithExtension();
//Assert
//...
}
So now when the outer is called and the extension method invoked, it will be acting on the mock controlled by you.
I am writing a testing framework for my system, which allows users to create mocked inputs to the system. The system manipulates that input, and updates some of its members for later post processing.
In order to allow users to mock the input, I need to be able to update a mocked input's members. Furthermore, the input may not be a mock, so I would prefer a solution oblivious to the type of element received.
Simply put, I have a function which receives an object and attempts to set one of its properties:
func(object a)
a.m = 5;
Which I want to test by mocking its input a, using the Moq library. Unfortunately, my test failed, since mocked objects' members need to be set using Mock.SetUpGet, instead of standard member assignment.
What would be a good way to test such a function, without changing it?
In order to be able to mock and test it, the property must be virtual, but if that's the case you can use the SetupSet method, rather than SetupGet:
public class A
{
public virtual int m { get; set; }
}
[TestFixture]
public class Tests
{
public static void SetProperty(A a)
{
a.m = 5;
}
[Test]
public void IsThePropertySet()
{
var x = new Mock<A>();
x.SetupSet<int>(a => a.m = 5).Verifiable();
SetProperty(x.Object);
x.Verify();
}
}
Your function should have getters and setters anyway.
And a good way of testing whether your functions work is to do a small-scale test. Initialize an object and send that in to your method, seeing whether it does what you want.
public class myClass
{
public int number;
public void setNumber(int nNum)
{
number = nNum;
}
public int getNumber()
{
return number;
}
}
class Program
{
static void myMethod(myClass obj, int nNumb)
{
//obj.setNumber(nNumb);
obj.number = nNumb;
}
static void Main(string[] args)
{
myClass myObj = new myClass();
myMethod(myObj, 3);
//Console.WriteLine(myObj.getNumber());
Console.WriteLine(myObj.number);
Console.ReadLine();
}
}
}
Output: 3
I have seen a lot posts explaining how to use NMock to expect an exception. But that is not what i want to know. In my case, i am testing the happy path. But looks like the NMock is throwing exceptions as long as the method called on the mock is called within a try/catch.
So suppose I have a method in the class i am testing:
class MyClass
{
public MyClass(some_type obj)
{
m_member = obj;
}
public void Func()
{
try
{
m_member.some_function()
}
catch (System.Exception e)
{
// do something
}
}
private some_type m_member;
}
In the unit test for this class, I have a test for this Func in MyClass to test the happy path:
[Test]
public void TestFunc()
{
MockFactory mock_factory = new MockFactory();
Mock<some_type> mock = mock_facoty.CreateMock<some_type>();
MyClass uut = new MyClass();
mock.Expects.One.MethodWith(_ => _.some_function());
uut.Func();
mock_facoty.VerifyAllExpectationsHaveBeenMet();
mock_facoty.ClearExpectations();
}
This unit test keeps failing. If I remove the try/catch in the code and just do (line 8 - 18):
public void Func()
{
//try
//{
m_member.some_function()
//}
//catch (System.Exception e)
//{
// // do something
//}
}
This test will work fine.
Does anyone have an idea as to why is this happening? and how could I make this work?
Thank you very much!
You are not actually using your mocked object, you are creating the concrete (real) some_type object in your MyClass constructor with the new statement.
I would suggest changing your MyClass class to accept the some_type object as a constructor parameter:
public MyClass(some_type thetype)
{
m_member = thetype;
}
Then, in your test method, pass the mocked object into the ctor:
MockFactory mock_factory = new MockFactory();
Mock<some_type> mock = mock_factory.CreateMock<some_type>();
MyClass uut = new MyClass(mock.MockObject);
That will mean that your MyClass instance will actually use the mocked object, and then you can verify against it...
If you can't change your constructor to have a required parameter all the time, you could use a poor man's dependency injection (not recommended but might be necessary):
public MyClass() : this(new some_type()) {}
public MyClass(some_type thetype)
{
m_member = thetype;
}
Here is the code:
public interface IAccessPoint
{
int BackHaulMaximum { get; set; }
bool BackHaulMaximumReached();
void EmailNetworkProvider();
}
public class AccessPoint : IAccessPoint
{
public int BackHaulMaximum { get; set; }
public bool BackHaulMaximumReached()
{
if (BackHaulMaximum > 80)
{
EmailNetworkProvider();
return true;
}
return false;
}
public void EmailNetworkProvider()
{
}
}
//Test
[Test]
public void NetworkProviderShouldBeEmailedWhenBackHaulMaximumIsReached()
{
var apMock = MockRepository.GenerateMock<IAccessPoint>();
apMock.Stub(x => x.BackHaulMaximum).Return(81);
Assert.AreEqual(true, apMock.BackHaulMaximumReached());
apMock.AssertWasCalled(x => x.EmailNetworkProvider());
}
You shouldn't be mocking the class that you are testing. You should only be mocking the classes that class under test depends on. Something like this:
public interface IAccessPoint
{
int BackHaulMaximum { get; set; }
bool BackHaulMaximumReached();
void EmailNetworkProvider();
}
public class AccessPoint : IAccessPoint
{
private IMailProvider Mailer { get; set; }
public AccessPoint( IMailProvider provider )
{
this.Mailer = provider ?? new DefaultMailProvider();
}
public int BackHaulMaximum { get; set; }
public bool BackHaulMaximumReached()
{
if (BackHaulMaximum > 80)
{
EmailNetworkProvider();
return true;
}
return false;
}
public void EmailNetworkProvider()
{
this.Mailer.SendMail(...);
}
}
[Test]
public void NetworkProviderShouldBeEmailedWhenBackHaulMaximumIsReached()
{
var mailerMock = MockRepository.GenerateMock<IMailProvider>();
mailerMock .Expect( m => m.SendMail( ... specify argument matches ... ) );
var accessPoint = new AccessPoint( mailerMock );
accessPoint.BackHaulMaximum = 81;
Assert.IsTrue( accessPoint.BackHaulMaximumReached() );
mailerMock.VerifyAllExpectations();
}
I agree with tvanfosson's answer. 99% (maybe 100%) of the time, you won't want a mock for your SUT.
However the reason it is failing is because your call:
Assert.AreEqual(true, apMock.BackHaulMaximumReached());
is not going to result in the actual method BackHaulMaximumReached() being called! It's going to call the mock's method.
Edit
If it wasn't clear, that means that BackHaulMaximumReached() will (I think) return the default value for its return type, which is "false" in the case of bools. So you need to stub out that method to return true to get the Assert.AreEqual to to pass (which is not a good test as I said).
Your next assertion will then fail, as EmailNetworkProvider() will never get called (again, because you are calling the mock's methods, not the actual SUT's methods).
I would change the code as follows:
//Test
[Test]
public void NetworkProviderShouldBeEmailedWhenBackHaulMaximumIsReached()
{
var mockRepo = new MockRepository();
var apMock = mockRepo.PartialMock<AccessPoint>();
using (mockRepo.Record())
{
Expect.Call(apMock.EmailNetworkProvider).Repeat.Once();
}
using (mockRepo.Playback())
{
apMock.BackHaulMaximum = 81;
Assert.AreEqual(true, apMock.BackHaulMaximumReached());
}
mockRepo.VerifyAll();
}
What you're expecting is that given the input to the class, when you call a method, another method that induces some unwanted side effects is also called. You want to mock the side-effect-inducing behavior, but you want to exercise the rest of the actual logic.
The solution is a PartialMock. It allows the mocking behavior to be applied to only the members of the mocked class that you specify. You can use it in several syntaxes, but the safest, most reliable method is to record and playback expectations rather than call the Expects() method on the mock itself.
We are using it here to expect a call on our method that we want to mock; expecting the call will cause the actual method not to be called. Then, the class is exercised, and the logic of the actual class is used (hence the assertion in our test succeeds), but when the call to our expected method is reached, what is actually called is the mocked method that updates some internal counters. Then, VerifyAll() asserts that all expectations happened according to the settings prescribed.
How can I test that a particular method was called with the right parameters as a result of a test? I am using NUnit.
The method doesn't return anything. it just writes on a file. I am using a mock object for System.IO.File. So I want to test that the function was called or not.
More context is needed. So I'll put one here adding Moq to the mix:
pubilc class Calc {
public int DoubleIt(string a) {
return ToInt(a)*2;
}
public virtual int ToInt(string s) {
return int.Parse(s);
}
}
// The test:
var mock = new Mock<Calc>();
string parameterPassed = null;
mock.Setup(c => x.ToInt(It.Is.Any<int>())).Returns(3).Callback(s => parameterPassed = s);
mock.Object.DoubleIt("3");
Assert.AreEqual("3", parameterPassed);
You have to use some mocking framework, such as Typemock or Rhino Mocks, or NMocks2.
NUnit also has a Nunit.Mock, but it is not well-known.
The syntax for moq can be found here:
var mock = new Mock<ILoveThisFramework>();
// WOW! No record/reply weirdness?! :)
mock.Setup(framework => framework.DownloadExists("2.0.0.0"))
.Returns(true)
.AtMostOnce();
// Hand mock.Object as a collaborator and exercise it,
// like calling methods on it...
ILoveThisFramework lovable = mock.Object;
bool download = lovable.DownloadExists("2.0.0.0");
// Verify that the given method was indeed called with the expected value
mock.Verify(framework => framework.DownloadExists("2.0.0.0"));
Also, note that you can only mock interface, so if your object from System.IO.File doesn't have an interface, then probably you can't do. You have to wrap your call to System.IO.File inside your own custom class for the job.
By using a mock for an interface.
Say you have your class ImplClass which uses the interface Finder and you want to make sure the Search function gets called with the argument "hello";
so we have:
public interface Finder
{
public string Search(string arg);
}
and
public class ImplClass
{
public ImplClass(Finder finder)
{
...
}
public void doStuff();
}
Then you can write a mock for your test code
private class FinderMock : Finder
{
public int numTimesCalled = 0;
string expected;
public FinderMock(string expected)
{
this.expected = expected;
}
public string Search(string arg)
{
numTimesCalled++;
Assert.AreEqual(expected, arg);
}
}
then the test code:
FinderMock mock = new FinderMock("hello");
ImplClass impl = new ImplClass(mock);
impl.doStuff();
Assert.AreEqual(1, mock.numTimesCalled);
In Rhino Mocks where is a method called AssertWasCalled
Here is a way to use it
var mailDeliveryManager = MockRepository.GenerateMock<IMailDeliveryManager>();
var mailHandler = new PlannedSending.Business.Handlers.MailHandler(mailDeliveryManager);
mailHandler.NotifyPrinting(User, Info);
mailDeliveryManager.AssertWasCalled(x => x.SendMailMessage(null, null, null), o => o.IgnoreArguments());