0

私の現在の解決策は、サービス/データビジネスロジックを持つクラスを作成し、それをローカルデータベース(mdf)でテストしてから、そのクラスをデータサービスクラスの同じ関数でラップすることです。

public class MyDataService : DataService<MyEntities>
{
    [WebGet]
    [SingleResult]
    public Team GetTeam(string name)
    {
        return _serviceBusinessLogic.GetTeam(name);
    }
}

//seam here to make this testable
public class ServiceBusinessLogic
{
    public Team GetTeam(string name)
    {
        return _dbContext.Teams.SingleOrDefault(p => p.Name == name);
    }
}

ただし、これらは同一であるため、ラッパー関数は必要ありません。

データサービスを直接テストしたいのですが、CreateDataSourceが保護されているため、データソースを設定する方法がありません。

public class MyDataService : DataService<MyEntities>
{
    [WebGet]
    [SingleResult]
    public Team GetTeam(string name)
    {
        //problem is CurrentDataSource is not settable, so cant set it in test
        return CurrentDataSource.Teams.SingleOrDefault(p => p.Name == name);
    }
}
4

1 に答える 1

0

次のように、データソースを注入できるようにクラスを作成できます。

public class MyDataService : DataService<MyEntities>
{
    private MyEntities _dataSource;

    public MyDataService() : this(new MyEntities()){}

    public MyDataService(MyEntities dataSource)
    {
        _dataSource = dataSource;
    }

    protected override MyEntities CreateDataSource()
    {
         return _dataSource;
    }

    [WebGet]
    [SingleResult]
    public Team GetTeam(string name)
    {
        return CurrentDataSource.Teams.SingleOrDefault(p => p.Name == name);
    }
}

ベースコンストラクターで呼び出された場合CreateDataSource、静的を使用して状態をクリアすることに注意する必要があるかもしれませんが、これはそのまま機能するに違いありません。

于 2013-03-24T19:51:11.130 に答える