I'm new in unit testing, Below is code for unit testing using xunit
public class FloorManager
{
public List<FloorInfo> Floors { get; }
public FloorManager()
{
Floors = new List<FloorInfo>();
SelectedFloor = -1;
}
public FloorInfo FindFloorByName(string name)
{
FloorInfo fInfo = Floors.Find(floor => floor.Name == name);
return fInfo;
}
}
public class FloorInfo
{
public String Name { get; set; }
}
I have a test for it:
[Fact]
public void FindFloorByName_ShouldGetName()
{
using (var mock = AutoMock.GetLoose())
{
string floorName = "First Floor";
var fInfo = new FloorInfo { Name = floorName };
mock.Mock<FloorManager>()
.Setup(x => x.FindFloorByName(floorName)).Returns(fInfo);
var cls = mock.Create<FloorManager>();
var expected = "First Floor";
var actual = cls.FindFloorByName(floorName);
Assert.True(expected == actual.Name);
}
}
but when i run test it gives me error :-
> `System.NotSupportedException : Unsupported expression: x => x.FindFloorByName(FloorManagerTests.<>c__DisplayClass0_0.floorName)`
Please give me solution how to test above function to pass test case
There is nothing to mock in the shown code example provided in the original question.
Also the subject under test (SUT) is usually not mocked when unit testing in isolation
Given the following class as an example
Some simple tests for the public facing members of the subject class
And as more complexity is added to the subject class, more tests can be added to verify expected behavior.
If the manager was instead dependent on a service
For example,
Then there would be a need to mock the dependency in order to test the subject in isolation.