0

I am trying with Simple Multiplication application ,

public virtual int Multi(int a, int b) { return a * b; } 

I am trying to Mock it using Moq. But in the

namespace UnitTestProject1 { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { int a = 5; int b = 10; Mock<WebForm1> Titi = new Mock<WebForm1>(); // WebForm1 obj = new WebForm1(); //int Real= obj.Multi(a, b); // Titi.Setup(x => x.data()).Returns(true); Titi.CallBase = true; var data= Titi.Setup(x => x.Multi(a, b)).Returns(50); Assert.AreEqual(true, Titi.Object); //Assert.AreEqual(50, Titi.Object); } } } 

Where as in the Mocking output I am getting

Assert.AreEqual failed. Expected:<True (System.Boolean)>. Actual:<Castle.Proxies.WebForm1Proxy (Castle.Proxies.WebForm1Proxy)> 

It means the actual & Expected are not matching, But why I am getting this error? where as it's a simple logic.

1 Answer 1

1

You are not using the mock correctly

[TestMethod] public void TestMethod1() { int a = 5; int b = 10; int expected = 50; Mock<WebForm1> mockWebForm = new Mock<WebForm1>(); mockWebForm.Setup(x => x.Multi(a, b)).Returns(expected); var webForm = mockWebForm.Object; var data = webForm.Multi(a, b); Assert.AreEqual(50, data); } 

Normally mocks are used for dependencies.

for example say you have

public interface IMultiply { int Multiply(int a, int b); } 

and your web form depends on that interface

public class WebForm1 { IMultiply multiplier; public WebForm1(IMultiply multiplier) { this.multiplier = multiplier; } public virtual int Multi(int a, int b) { return multiplier.Multiply(a, b); } } 

then a unit test can look like this

[TestMethod] public void TestMethod1() { //Arrange int a = 5; int b = 10; int expected = 50; var mockMultiplier = new Mock<IMultiply>(); mockMultiplier.Setup(x => x.Multiply(a, b)).Returns(expected); //your web form is the system under test var webForm = new WebForm1(mockMultiplier.Object); //Act var actual = webForm.Multi(a, b); Assert.AreEqual(expected, actual); } 
Sign up to request clarification or add additional context in comments.

1 Comment

@lokanathdas has you issue been resolved? If this resolved you problem, mark it as the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.