Reputation: 9669
So to learn more about unit tests and mocking i created the following simple interface which i want to mock and test in my unit test:
namespace TestProjekt
{
public interface ICsvFile
{
string FileName { get; }
int GetFileSize();
}
}
and the a test using NUnit and Moq
namespace NUnitTests
{
using Moq;
using NUnit.Framework;
using TestProjekt;
[TestFixture]
public class UnitTests1
{
private const string FILENAME = "0030001744_14224429_valuereport_20140527000012_1104.csv";
private const int FILESIZE = 155;
[Test]
public void ExampleTest()
{
var file = new Mock<ICsvFile>();
file.Setup(m => m.GetFileSize()).Returns(FILESIZE);
file.SetupGet(m => m.FileName).Returns(FILENAME);
Assert.AreEqual(FILENAME, file.FileName);
Assert.AreEqual(FILESIZE, file.GetFileSize);
}
}
}
I made this based on a Tutorial i found online and quite interesting. The Problem is, that Visual Studio cant resolve the Method and Property or anything else i setup
on the mocked object.
All dlls are referenced correctly. It must be a super simply thing but i just cant figure it out. Thanks in advance!
Upvotes: 1
Views: 2437
Reputation: 4344
I guess that you encountered a compile error. The reason is that you used the properties directly from the instance of the Mock<ICsvFile>
type. Instead, you should use like file.Object.FileName
. The Object
property represents the mocked instance of ICsvFile
.
Modify your code as the following.
Assert.AreEqual(FILENAME, file.Object.FileName);
Assert.AreEqual(FILESIZE, file.Object.GetFileSize);
Upvotes: 3