First Create a void method with a ref parameter in your Class Library Solution.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Class1
{
public void methodRef(ref int refvalue)
{
refvalue= refvalue + refvalue;
}
}
}
Out goal is to test this void method in unit test.
Calling the methodRef in Test Class.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClassLibrary1;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMetod3()
{
int refvalue = 10;
ClassLibrary1.Class1 obj = new ClassLibrary1.Class1();
obj.methodRef(ref refvalue);
int actualvalue = refvalue;
Assert.AreEqual(20, actualvalue);
}
}
}
Build the solution and Run TestMethod3. It will give the actual Result as 20 and Expected Result as 20.

John OralePosted May 3, 2021, 12:01 AM
This will fail, your code does not catch the changes happened in refvalue so it remains 10. basically not equal to 20
Akash VarshneyPosted Oct 1, 2018, 7:38 AM
I am not pretty convinced to this approach. what we are doing for return type void ????
souvik sardarPosted Aug 16, 2018, 11:17 AM
How to do the same without ref. like if my method is like this: public void methodRef(ref int refvalue) { refvalue= refvalue + refvalue; }