I need to test simple class like this:
public class SomeEntity {
public int Id { get; set; }
public string Name { get; set; }
}
And I want to write test using mock of this class objects like this:
var someMock = new Moq.Mock<SomeEntity>();
someMock.SetupGet(x => x.Id).Returns(12345678);
someMock.SetupGet(x => x.Name).Returns(It.IsIn(someList));
As you know Moq framework require interface or virtual methods to create proxy object. So my question is: how can I mock my entity class without implementing interface or marking properties with virtual?
UPDATE: what is the most desired result of all this is to substitute moq object to my unit test with multiple possible combinations of properties.
UPDATE 2: it looks like I don't need to mock my entity class, I just need to iterate via all possible combinations using It.IsIn(someList):
var someEntity = new SomeEntity {
Id = 12345678,
Name = It.IsIn(someList)
}
You wouldn't need to mock this to be able to test with it.
You should be able to test with SomeEntity in its current state directly (and test it itself but testing auto properties seems like a waste of effort).
If you create an interface of ISomeEntity that SomeEntity implements then you would be able to mock that inside anything that consumes it. Then you would be able to test the behavior of methods that an ISomeEntity is passed into.
As you've said Moq requires it to be an interface or virtual methods to work on.
public class SomeEntity : ISomeEntity {
public int Id { get; set; }
public string Name { get; set; }
}
public interface ISomeEntity {
int Id { get; set; }
string Name { get; set; }
}
Then
var someMock = new Mock<ISomeEntity>();
someMock.SetupGet(x => x.Id).Returns(12345678);
someMock.SetupGet(x => x.Name).Returns(It.IsIn(someList));
var result = somethingelse.Act(someMock.Obect);
If your object is as logic free as in the example then there is no need to go to Mock
var item = new SomeEntity();
item .Id = 12345678;
item .Name = "some name";
var result = somethingelse.Act(item);
What benefit are you trying to get via using a mock here? If you are trying to run through the list and verfify for each item in it, It.IsIn won't do that (even attached to a mock) all it will check is that the value assigned to the mock is in the list of allowed values.
This breaks the single assert for each test maxim but, I think this is what you want:
var item = new SomeEntity();
foreach (var name in someList)
{
item .Id = 12345678;
item .Name = name ;
var result = somethingelse.Act(item);
// Assert
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With