我的类/接口设置如下:
Room.cs
//import statements
namespace namespace1
{
internal class Room: Apartment
{
// constructor
public Room(Furniture furniture) : base(furniture)
{
}
}
}
公寓.cs
// import statements
namespace namespace2
{
public abstract class Apartment: Building
{
private int numChairs = 0;
// constructor
protected Apartment(IFurniture furniture) : base(IFurniture furniture)
{
this.numChairs = furniture.chairs.Length;
}
}
}
建筑.cs
// import statements
namespace namespace3
{
public abstract class Building
{
// constructor
protected Building(IFurniture furniture)
{
}
}
}
我想创建一个房间对象,带有一个模拟家具对象。以下是我所做的:
[Test]
public void fun()
{
var mockedFurniture = new Mock<IFurniture>();
var room = new Room(mockedFurniture.Object);
}
问题:因为的构造函数
Room
电话
base(furniture)
,的构造函数
Apartment
正在尝试访问
furniture.chairs
,即
null
。我怎么能嘲笑这个?
编辑
问题在于
Apartment.cs
.它试图访问家具.chairs
无效的
。这是IFurniture.cs:
public interface IFurniture
{
IChairs Chairs { get; }
}