Tag: rhino mocks

Rhino Mocks – 测试存储库层返回“对象引用未设置为实例”错误

谨慎的是,我首先说我是犀牛模拟的新手,而且更普遍地嘲笑。 考虑到这一点,我正在尝试对我的Linq to SQL存储库层进行unit testing,以确保正在命中datacontext上的正确方法,并确保LINQ to SQL正确过滤。 〜为了清晰起见〜 有问题的方法 – ‘GetRecordWhere’ – 在Repository类中定义。 它在DataContextWrapper上调用方法 – ‘GetTable’ – 它是我的Linq to SQL DataContext(自动生成)的自定义包装器,它被实现以使DataContext可模拟。 public interface IDataContextWrapper : IDisposable { IQueryable GetTable() where TName : class; } public class DataContextWrapper : IDataContextWrapper { public IQueryable GetTable() where TName : class { return _db.GetTable().AsQueryable(); } } public class Repository […]

使用RhinoMocks测试私有方法

我在TDD环境中工作,基本上我面临着一个两难的问题,我认为这在TDD环境中非常重要。 作为程序员,您希望您的方法尽可能可读。 为实现这一目标,我们倾向于在多个私有方法中对方法进行分区。 在这样做时,所有移动到私有函数的代码都失去了它的测试能力。 Rhino测试类无法看到所有这些私有方法,我也需要能够针对这些方法运行测试。 我不希望它们被公开,因为将它们公开是没有意义的。 有任何想法吗?

不能使用带有数组参数的构造函数使用Rhino Mocks来模拟类

我们无法在RhinoMocks中模拟这个类。 public class Service { public Service(Command[] commands){} } public abstract class Command {} // Code var mock = MockRepository.GenerateMock(new Command[]{}); // or mock = MockRepository.GenerateMock(null) Rhino模拟失败抱怨它找不到具有匹配参数的构造函数。 我究竟做错了什么? 谢谢,

犀牛嘲笑:如何建立假套接字?

我尝试使用以下代码构建一个假套接字进行测试: var socket = MockRepository.GenerateStub( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP ); socket.Stub(v => v.RemoteEndPoint).PropertyBehavior().Return( new IPEndPoint(IPAddress.Parse(“127.0.0.1”), 12345) ); 但是,为只读属性创建存根的尝试给出了以下exception: 呼叫无效,最后一次呼叫已被使用或未进行任何呼叫(确保您正在调用虚拟(C#)/可覆盖(VB)方法)。 任何人都可以帮助我找出它出错的地方吗? 谢谢

具有匿名对象的Arg .Is.Equal

在我的MVC3项目中,我使用IUrlProvider接口来包装UrlHelper类。 在我的一个控制器操作中,我有这样的调用: string url = _urlProvider.Action(“ValidateCode”, new { code = “spam-and-eggs” }); 我想在我的unit testing中存根这个方法调用,这是在一个单独的项目中。 测试设置看起来像这样: IUrlProvider urlProvider = MockRepository.GenerateStub(); urlProvider.Stub(u => u.Action( Arg.Is.Equal(“ValidateCode”), Arg.Is.Equal(new { code = “spam-and-eggs” }) )) .Return(“http://www.mysite.com/validate/spam-and-eggs”); 不幸的是, Arg.Is.Equal(new { code = “spam-and-eggs” })不起作用,因为new { code = “spam-and-eggs” } != new { code = “spam-and-eggs” }在不同的程序集中声明匿名类型时。 那么,是否有一种替代语法可以与Rhino Mocks一起使用来检查跨程序集的匿名对象之间的匹配字段值? 或者我应该用类替换匿名对象声明,像这样? public class […]

c#中的模拟文件IO静态类

我是Unit Testing的新手,我需要在System.IO命名空间中模拟File静态类。 我正在使用Rhinomock,实现这一目标的最佳方法是什么, 让我说我需要模拟File.Exists,File.Delete ……

RhinoMocks模拟方法没有返回

我是嘲笑的新手。 我需要模拟方法(它没有返回值)。 我找不到任何模拟方法的例子。 我需要模拟ITempDa.Import方法。 var stub = MockRepository.GenerateStub(); stub.Stub(x => x.Import(param1)). ??? public void MockedImport() { // some processing here } ITempDa.Import应该被ITempDa.Import ,而应该调用一些内部方法“ MockedImport ”。

使用Rhino Mocks对Func 进行unit testing,并使用第二个Expect()调用抛出InvalidCastException

我正在测试一个获取Autofac注入的参数的类。 一些参数是Func 。 这允许我创建多个委托使用。 如果您需要更好的描述,请参阅此Autofac Wiki页面 。 这是课堂的一部分。 public class CreateProductCommand : TransactionalDBCommandBase { public delegate CreateProductCommand Factory(ProductInfo info, IAppSecurityContext context); private ProductInfo _info; private Func _saveTextMasterCommandFactory; private Func _saveTextValueCommandFactory; private SaveProductCommand.Factory _saveProductCommand; public CreateProductCommand(ProductInfo info, Func saveTextMasterCommandFactory, Func saveTextValueCommandFactory, SaveProductCommand.Factory saveProductCommand, IAppSecurityContext context) { this._info = info; this._saveTextMasterCommandFactory = saveTextMasterCommandFactory; this._saveTextValueCommandFactory = saveTextValueCommandFactory; this._saveProductCommand […]

如何测试抽象类的受保护抽象方法?

我一直在研究测试名为TabsActionFilter的抽象类的最佳方法。 我已经保证从TabsActionFilterinheritance的类将有一个名为GetCustomer的方法。 在实践中,这种设计似乎运作良好。 我遇到的一些问题是如何测试基类的OnActionExecuted方法。 此方法依赖于受保护的抽象 GetCustomer方法的实现。 我曾尝试使用Rhino Mocks嘲笑这个类,但似乎无法模仿GetCustomer假客户的回报。 显然,将方法翻转为公共将使模拟可用,但受保护感觉更合适的可访问性级别 。 暂时在我的测试类中,我添加了一个inheritance自TabsActionFilter的具体私有类,并返回一个伪造的Customer对象。 具体类是唯一的选择吗? 是否有一个简单的模拟机制,我错过了允许Rhino Mocks为GetCustomer提供回报? 作为一个注释, Anderson Imes在关于Moq的回答中讨论了他对此的看法,我可能会遗漏一些关键,但这似乎不适用于此。 需要测试的类 public abstract class TabsActionFilter : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { Customer customer = GetCustomer(filterContext); List tabItems = new List(); tabItems.Add(CreateTab(customer, “Summary”, “Details”, “Customer”, filterContext)); tabItems.Add(CreateTab(customer, “Computers”, “Index”, “Machine”, filterContext)); tabItems.Add(CreateTab(customer, “Accounts”, “AccountList”, “Customer”, filterContext)); […]

在犀牛嘲笑中嘲弄lambda

我正在尝试使用Rhino Mocks来模拟下面的lambda,但是不断碰到一堵砖墙 var result = rep.Find(x => (x as IEntity).ID == (entity as IEntity).ID).FirstOrDefault(); 有任何想法吗?