You haven't assigned a value to the mock field. Add the following line to your test.
Mock<Class1> mock = new Mock<Class1>()
You won't be able to mock that method anyway because it's not virtual. See this question for more info: Moq: Invalid setup on a non-overridable member: x => x.GetByTitle("asdf")
Finally, the behaviour you set up only applies to the instance that you retrieve from your mock with the mock.Object
property, not to normal instances of Class1
. So if you instantiate a new Class1()
, you won't get the mocked behaviour.
In summary, your code would have to look more like this:
public class Class1
{
public virtual string GetHelloWorld()
{
return "HelloWorld";
}
}
public class UnitTest1
{
[Test]
public void TestMethod1()
{
//arrange
Mock<Class1> mock = new Mock<Class1>();
mock.Setup(m => m.GetHelloWorld()).Returns(()=> "Hi");
Class1 obj = mock.Object;
//act
string x = obj.GetHelloWorld();
//assert
Assert.AreEqual("Hi", x);
}
}