6

私が現在取り組んでいる .net 3.5 プロジェクトでは、サービス クラスのテストをいくつか書いていました。

public class ServiceClass : IServiceClass
{
     private readonly IRepository _repository;

     public ServiceClass(IRepository repository)
     {
          _repository = repository;
     }

     #region IServiceClass Members

     public IEnumerable<ActionType> GetAvailableActions()
     {
         IQueryable<ActionType> actionTypeQuery = _repository.Query<ActionType>();
         return actionTypeQuery.Where(x => x.Name == "debug").AsEnumerable();
     }

     #endregion
}

そして、私はスタブまたはモックの方法を理解するのに苦労していました

actionTypeQuery.Where(x => x.Name == "debug")

部。

これが私がこれまでに得たものです:

[TestFixture]
public class ServiceClassTester
{
    private ServiceClass _service;
    private IRepository _repository;
    private IQueryable<ActionType> _actionQuery;
    [SetUp]
    public void SetUp()
    {
        _repository = MockRepository.GenerateMock<IRepository>();
        _service = new ServiceClass(_repository);
    }

    [Test]
    public void heres_a_test()
    {
        _actionQuery = MockRepository.GenerateStub<IQueryable<ActionType>>();

        _repository.Expect(x => x.Query<ActionType>()).Return(_actionQuery);
        _actionQuery.Expect(x => x.Where(y => y.Name == "debug")).Return(_actionQuery);

        _service.GetAvailableActions();

        _repository.VerifyAllExpectations();
        _actionQuery.VerifyAllExpectations();
    }

}

[注: 罪のない人を保護するためにクラス名が変更されています]

しかし、これはSystem.NullReferenceExceptionat で失敗します

_actionQuery.Expect(x => x.Where(y => y.Name == "debug")).Return(_actionQuery);

だから私の質問は:

RhinoMocks を使用して IQueryable.Where 関数をモックまたはスタブし、このテストに合格するにはどうすればよいですか?

現在のセットアップで IQueryable をモックまたはスタブ化できない場合は、理由を説明してください。

この壮大な長い質問を読んでくれてありがとう。

4

4 に答える 4

6

Rhino モックを使用せずに、List を作成し、それに対して .AsQueryable() を呼び出すことができます。例えば

var actionTypeList = new List<ActionType>() {
    new ActionType {},  //put your fake data here
    new ActionType {}
};

var actionTypeRepo = actionTypeList.AsQueryable();

これにより、少なくとも偽のリポジトリが取得されますが、メソッドが呼び出されたことを確認することはできません。

于 2009-04-26T17:52:15.180 に答える
6

Where拡張メソッドです。これは、インターフェイス IQueriable によって実装されるメソッドではありません。IQueriable のメンバーを見てください: http://msdn.microsoft.com/en-us/library/system.linq.iqueryable_members.aspx

拡張メソッドは静的であり、モックできません。WhereIMO、それは言語の一部であるため、モックする必要はありません。リポジトリのモックのみを作成する必要があります。

編集、例:

[TestFixture]
public class ServiceClassTester
{
    private ServiceClass _service;
    private IRepository _repository;
    private IQueryable<ActionType> _actionQuery;

    [SetUp]
    public void SetUp()
    {
        _service = new ServiceClass(_repository);

        // set up the actions. There is probably a more elegant way than this.
        _actionQuery = (new List<ActionType>() { ActionA, ActionB }).AsQueryable();

        // setup the repository
        _repository = MockRepository.GenerateMock<IRepository>();
        _repository.Stub(x => x.Query<ActionType>()).Return(_actionQuery);
    }

    [Test]
    public void heres_a_test()
    {
        // act
        var actions = _service.GetAvailableActions();

        // assert
        Assert.AreEqual(1, actions.Count());
        // more asserts on he result of the tested method
    }

}

注: メソッドはモックの戻り値に依存するため、呼び出しを期待する必要はありません。それを呼び出さない場合、アサートで失敗します。これにより、テストの保守が容易になります。

于 2009-04-27T00:26:58.990 に答える
5

私は当初、「IQueryable.Where(Func)」のような呼び出しもモックアウトしたかったのですが、間違ったレベルでテストしていると思います。代わりに、テストでは IQueryable をモックアウトして、結果を確認しました。

// Setup a dummy list that will be filtered, queried, etc
var actionList = new List<ActionType>()
{
    new ActionType() { Name = "debug" },
    new ActionType() { Name = "other" }
};
_repository.Expect(x => x.Query<ActionType>()).Return(actionList);

var result = _service.GetAvailableActions().ToList();

// Check the logic of GetAvailableActions returns the correct subset 
// of actionList, etc:
Assert.That(result.Length, Is.EqualTo(1));
Assert.That(result[0], Is.EqualTo(actionList[0]);

_repository.VerifyAllExpectations();
于 2009-04-27T00:51:58.570 に答える
0

文字列を含むFindメソッドに述語式をスタブしようとした同様の問題がありましたが、文字列は参照型であり、もちろん不変です。スタブ、実際のSUT、そして最終的にアサートするために渡すtestPredicateを作成するまで成功しません。以下のコードは機能します。

    [Test()]
    public void Search_Apartment_With_Spesific_Address()
    {
        //ARRANGE
        var repositoryMock = MockRepository.GenerateMock<IApartmentRepository>();
        var notificationMock = MockRepository.GenerateMock<INotificationService>();
        var service = new ApartmentService(repositoryMock, notificationMock);
        var apartment = new List<Apartment> {new Apartment {Address = "Elm Street 2"}}.AsQueryable();

        Expression<Func<Apartment, bool>> testPredicate = a => a.Address == "Elm Street 2";
        repositoryMock.Stub(x => x.Find(testPredicate)).Return(apartment);

        //ACT
        service.Find(testPredicate);

        //ASSERT
        repositoryMock.AssertWasCalled(x => x.Find(testPredicate));
    }
于 2012-11-15T20:12:59.623 に答える