Rhino Mocks AAA構文を学習しようとしていますが、特定のメソッド(任意の引数値を使用)が呼び出されたことを表明するのに問題があります。テストフレームワークとしてMachine.Specificationsを使用しています。
この特定のメソッドは一般的であり、3つの異なるタイプで3回呼び出されたことを確認したいと思います。
repo.Save<T1>(anything), repo.Save<T2>(anything), and repo.Save<T3>(anything)
タイプごとに関数をスタブしました。しかし、私は興味深い結果を得ています。(下)
[Subject("Test")]
public class When_something_happens_with_constraint
{
static IRepository repo;
static TestController controller;
static ActionResult result;
Establish context = () =>
{
repo = MockRepository.GenerateMock<IRepository>();
controller = new TestController(repo);
repo.Stub(o => o.Save<Something>(Arg<Something>.Is.Anything));
repo.Stub(o => o.Save<SomethingElse>(Arg<SomethingElse>.Is.Anything));
repo.Stub(o => o.Save<AnotherOne>(Arg<AnotherOne>.Is.Anything));
};
//post data to a controller
Because of = () => { result = controller.SaveAction(new SomethingModel() { Name = "test", Description = "test" }); };
//controller constructs its own something using the data posted, then saves it. I want to make sure three calls were made.
It Should_save_something = () => repo.AssertWasCalled(o => o.Save<Somethign>(Arg<Something>.Is.Anything));
It Should_save_something_else = () => repo.AssertWasCalled(o => o.Save<SomethingElse>(Arg<SomethingElse>.Is.Anything));
It Should_save_another_one = () => repo.AssertWasCalled(o => o.Save<AnotherOne>(Arg<AnotherOne>.Is.Anything));
}
結果は、2つの例外と1つのパスです。
最初の呼び出しは次のようにスローします。
System.InvalidOperationException:検証する期待値が設定されていません。アクションのメソッド呼び出しが仮想(C#)/オーバーライド可能(VB.Net)メソッド呼び出しであることを確認してください
2番目のものはスローします:
System.InvalidOperationException:記録中のモックメソッド呼び出し内でのみArgを使用してください。1つの引数が必要で、2つが定義されています。
3番目のものは合格します...いくつかの奇妙な理由で。
また、セットアップでExpectでGenerateMock()を使用したり、StubでGenerateStub()を使用したりしました。どちらもまったく同じ結果になりました。私は何か間違ったことをしなければならない。
私が使用しているのは、MachineSpec0.3.0.0とRhinoMocks3.6.0.0です。
何か案は?
- - -修繕 - - - - -
これがリーの助けを借りた完全な(作業バージョン)です。追加の(非linq)レイヤーを使用しています。私の実際の問題は、私のテストの1つがオフラインの実際のコードで間違ったラムダ変数を再利用したことでした。それはShould_do_something=()=> repo.AssertWasCalled(o => repo .Save(data)); //悪いラムダ
したがって、参照用の正しいテストのサンプルを次に示します。
using System;
using System.Linq;
using System.Collections.Generic;
using Machine.Specifications;
using Rhino.Mocks;
namespace OnlineTesting.Specifications
{
public interface Repository
{
void Save<T>(T data);
IQueryable<T> All<T>();
}
public interface Service
{
void SaveItem(Item data);
void SaveAnotherItem(AnotherItem data);
void SaveOtherItem(OtherItem data);
List<Item> GetItems();
List<AnotherItem> GetAnotherItems();
List<OtherItem> GetOtherItems();
}
public class ConcreteService : Service
{
Repository repo;
public ConcreteService(Repository repo)
{
this.repo = repo;
}
public void SaveItem(Item data)
{
repo.Save(data);
}
public void SaveAnotherItem(AnotherItem data)
{
repo.Save(data);
}
public void SaveOtherItem(OtherItem data)
{
repo.Save(data);
}
public List<Item> GetItems()
{
return repo.All<Item>().ToList();
}
public List<AnotherItem> GetAnotherItems()
{
return repo.All<AnotherItem>().ToList();
}
public List<OtherItem> GetOtherItems()
{
return repo.All<OtherItem>().ToList();
}
}
public class Item
{
public int Id { get; set; }
}
public class OtherItem
{
}
public class AnotherItem
{
}
public class When_something_else_happens
{
Establish context = () =>
{
_repository = MockRepository.GenerateMock<Repository>();
_service = new ConcreteService(_repository);
_controller = new TestController(_service);
_repository.Stub(o => o.Save<Item>(Arg<Item>.Is.Anything)).WhenCalled(
new Action<MethodInvocation>((o) =>
{
var data = o.Arguments.FirstOrDefault() as Item;
if (data != null && data.Id == 0)
data.Id++;
}));
};
Because of = () => _controller.DoSomethingElse();
It should_save_the_first_thing = () =>
_repository.AssertWasCalled(repo => repo.Save(Arg<Item>.Is.Anything));
It should_save_the_other_thing = () =>
_repository.AssertWasCalled(repo => repo.Save(Arg<OtherItem>.Is.Anything));
It should_save_the_last_thing = () =>
_repository.AssertWasCalled(repo => repo.Save(Arg<AnotherItem>.Is.Anything));
static Repository _repository;
static TestController _controller;
static Service _service;
}
public class TestController
{
readonly Service _service;
public TestController(Service service)
{
_service = service;
}
public void DoSomethingElse()
{
_service.SaveItem(new Item());
_service.SaveOtherItem(new OtherItem());
_service.SaveAnotherItem(new AnotherItem());
}
}
}