C# 重构从Petapoco生成的项目

C# 重构从Petapoco生成的项目,c#,unit-testing,mocking,refactoring,petapoco,C#,Unit Testing,Mocking,Refactoring,Petapoco,我正在使用PetaPoco作为微型ORM创建一个C#Win表单应用程序。我想重构生成的类,使它们易于测试和模拟。 当我开始编写单元测试时,我被Mock卡住了,因为我不使用接口,但我不知道如何将接口与当前类集成 这是课程 public partial class League : FutbolDB.Record<League> { public int? id { get; set; } public string Name { get; set; } publ

我正在使用PetaPoco作为微型ORM创建一个C#Win表单应用程序。我想重构生成的类,使它们易于测试和模拟。 当我开始编写单元测试时,我被Mock卡住了,因为我不使用接口,但我不知道如何将接口与当前类集成

这是课程

public partial class League : FutbolDB.Record<League>
{
    public int? id { get; set; }
    public string Name { get; set; }
    public int? CantTeams { get; set; }
}

public partial class Team : FutbolDB.Record<Team>
{
    public int? id { get; set; }
    public string Name { get; set; }
    private int? LeagueId { get; set; }
}
公共部分课程联盟:FutbolDB.记录
{
公共int?id{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
}
公共部分班团队:FutbolDB.记录
{
公共int?id{get;set;}
公共字符串名称{get;set;}
私有int?LeagueId{get;set;}
}
然后我扩展这个类

public partial class Team : FutbolDB.Record<Team>
{
    private League _league; 

    public Team() {}

    public Team(string name, League league)
    {
        if (string.IsNullOrEmpty(name))
            throw new ArgumentException("The name cannot be empty or null");
        if (league == null)
            throw new ArgumentException("The League cannot be null");
        Name = name;
        League = league;
    }

    [Ignore]
    public League League
    {
        get { return _league ?? League.GetById(LeagueId); }
        set {
            _league = value;
            LeagueId = _league?.id;
        }
    }
公共部分课程团队:FutbolDB.记录
{
私人联赛;
公共团队(){}
公共团队(字符串名称,联赛)
{
if(string.IsNullOrEmpty(name))
抛出新ArgumentException(“名称不能为空或null”);
if(league==null)
抛出新的ArgumentException(“联盟不能为空”);
名称=名称;
联盟=联盟;
}
[忽略]
公众联盟
{
获取{return\u league??league.GetById(LeagueId);}
设置{
_联盟=价值;
LeagueId=_league?.id;
}
}
现在当我进行单元测试时

    //this runs fine
    [TestMethod()] 
    public void EqualsTest()
    {
        Mock<League> mockLeague = new Mock<League>();

        var t1 = new Team("team",mockLeague.Object);
        var t2 = new Team("Team", mockLeague.Object);

        t1.id = 1;
        t2.id = 1;

        Assert.IsTrue(t1.Equals(t1));
        Assert.IsFalse(t1.Equals(null));
        Assert.IsTrue(t1.Equals(t2));
    }
//运行良好
[TestMethod()]
public void equaltest()
{
Mock mockLeague=新Mock();
var t1=新团队(“团队”,mockLeague.Object);
var t2=新团队(“团队”,mockLeague.Object);
t1.id=1;
t2.id=1;
Assert.IsTrue(t1.Equals(t1));
Assert.IsFalse(t1.Equals(null));
断言IsTrue(t1等于(t2));
}
问题是,当我试图将该方法从联盟中剔除时,没有接口的模拟投掷和错误

    public TeamTests()
    {
        List<Team> teams = new List<Team>();
        teams.Add(new Team("barca", mockLeague.Object));
        teams.Add(new Team("atletico", mockLeague.Object));
        teams.Add(new Team("madrid", mockLeague.Object));

        // Error stubbing this method from a concrete class
        mockLeague.Setup(x => x.CantTeams).Returns(teams.Count);
    }
publicteamtests()
{
列表团队=新列表();
添加(新球队(“巴萨”,mockLeague.Object));
添加(新球队(“马竞”,mockLeague.Object));
添加(新团队(“马德里”,mockLeague.Object));
//从具体类中存根此方法时出错
mockLeague.Setup(x=>x.CantTeams.Returns(teams.Count);
}

单元测试、模拟和模式对我来说都是新事物,我目前正在尝试学习它们。提前感谢您尝试为您编写单元测试RModel,它说“当我创建一个id为1的模型时,id为1”。虽然您很荣幸,但这基本上是在测试.net framework内存属性(甚至是CLR的)。这与测试
var a=42;Assert.AreEqual(42,a);
相同。当然,该测试总是会通过,但它有用吗?测试存在于测试代码的
逻辑。声明属性不是逻辑。但是,如果您想测试模型,请使用反射(参见下一条注释)
Assert.AreEqual(typeof(int?),typeof(League).GetProperty(“id”,BindingFlags.Instance | BindingFlags.Public).PropertyType))
谢谢你的回答,现在我只测试对象是否为空,是否等于另一个。然后使用上面的示例?我明白了为什么与测试无关,而对象等于自身,所以我删除了它。现在我不需要检查ID也不需要使用反射。保持简单。谢谢,但我只是在学习,eas我现在走了两步。