c# - Trying with Mocking in Unit test case -


i trying simple multiplication application ,

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

i trying mock using moq. in

namespace unittestproject1 {     [testclass]     public class unittest1     {         [testmethod]         public void testmethod1()         {             int = 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 in mocking output getting

assert.areequal failed. expected:<true (system.boolean)>. actual:<castle.proxies.webform1proxy (castle.proxies.webform1proxy)> 

it means actual & expected not matching, why getting error? it's simple logic.

you not using mock correctly

[testmethod] public void testmethod1() {     int = 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 used dependencies.

for example have

public interface imultiply {     int multiply(int a, int b); } 

and web form depends on 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 unit test can this

[testmethod] public void testmethod1() {     //arrange     int = 5;     int b = 10;     int expected = 50;     var mockmultiplier = new mock<imultiply>();     mockmultiplier.setup(x => x.multiply(a, b)).returns(expected);      //your web form system under test     var webform = new webform1(mockmultiplier.object);      //act     var actual = webform.multi(a, b);      assert.areequal(expected, actual); } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -