我有以下 repo 接口,其中 FindBy 方法采用谓词
public interface IRepo<T> where T : class
{
IList<T> GetAll();
IList<T> FindBy(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Edit(T entity);
}
我正在尝试测试我的控制器是否确实调用了此方法。这是我的控制器代码
// GET:/Assets/EditAssets
public PartialViewResult EditAsset(Guid id)
{
var asset = _assetRepo.FindBy(ass => ass.Id == id).FirstOrDefault();
if (asset == null)
{
return PartialView("NotFound");
}
SetUpViewDataForComboBoxes(asset.AttachedDeviceId);
return PartialView("EditAsset", asset);
}
这是我的测试方法
[TestMethod]
public void editAsset_EmptyRepo_CheckRepoCalled()
{
//Arrange
var id = Guid.NewGuid();
var stubAssetRepo = MockRepository.GenerateStub<IRepo<Asset>>();
stubAssetRepo.Stub(x => x.FindBy(ass => ass.Id == id)).Return(new List<Asset> {new Asset()});
var adminAssetsControler = new AdminAssetsController(stubAssetRepo, MockRepository.GenerateStub<IRepo<AssetModel>>(), MockRepository.GenerateStub<IRepo<AssetSize>>(), MockRepository.GenerateStub<IRepo<AssetType>>(), MockRepository.GenerateStub<IRepo<Dept>>(), MockRepository.GenerateStub<IRepo<Device>>());
//Act
var result = adminAssetsControler.EditAsset(id);
//Assert
stubAssetRepo.AssertWasCalled(rep => rep.FindBy(ass => ass.Id == id));
}
但我得到一个argumentNullException。我之前在没有谓词的方法上做过这种测试,它工作正常。那么这个是怎么回事呢?
有没有设置这种测试的好方法?