3

I'm trying to unit test file read operations. In this scenario I also need make sure that, if a particular user don't have read access he should get an exception...

But somehow I'm unable to get it working, can anyone suggest something?

PS: I'm using Rhino mock and NUnit

4

3 回答 3

2

You could use Rhino.Mocks "Do" extension to throw a specific exception:

public delegate void ThrowExceptionDelegate();
mystub.Stub(x => x.ReadFile()).Do(new ThrowExceptionDelegate(delegate()
    { throw new IOException(); }
    ));

This would allow you to test your exception handling code.

于 2009-12-03T18:36:13.930 回答
1

我会进行适当的验收测试 - 过多地使用模拟可能有点危险。在这种情况下,很容易以编程方式设置 + 取消设置文件权限。

我有一个类似的问题 - 想测试一个权限问题 + 想出了以下帮助类来包装库 API 以弄乱文件权限

为所有用户设置 c:\program files\company\app\file 的文件权限

于 2012-03-26T16:09:30.747 回答
1

您需要进行适当的测试,而不是读取文件,而是使用引发异常而不是真正读取文件的模拟。然后,您可以验证是否触发了适当的处理并且事情正常进行。

如果您需要更好的答案,您需要给出您的类的示例,并且可能是您迄今为止编写的测试的框架。

于 2009-12-03T16:44:46.460 回答