如何在 NUnit 中对文件权限进行单元测试?
我正在尝试对文件读取操作进行单元测试。在这种情况下,我还需要确保,如果特定用户没有读取访问权限,他应该得到一个异常...
但不知何故我无法让它工作,任何人都可以提出建议吗?
PS:我正在使用Rhino模拟和NUnit
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用Rhino.Mocks“Do”扩展来抛出特定的异常:
这将允许您测试异常处理代码。
You could use Rhino.Mocks "Do" extension to throw a specific exception:
This would allow you to test your exception handling code.
您需要进行适当的测试,使用抛出异常的模拟来代替读取文件,而不是真正读取文件。然后您可以验证是否触发了适当的处理并且事情按预期进行。
如果您需要更好的答案,您需要提供您的类的示例,也许还提供您迄今为止编写的测试的框架。
You need to get a test in place which, in place of reading a file is using a mock that throws an exception instead of really reading a file. Then you can verify that the appropriate handling is triggered and things work out as they should.
If you need a better answer, you need to give an example of your classes and maybe the skeleton of the test you've written so far.
我会进行适当的验收测试 - 过多使用模拟可能会有点危险。在这种情况下,无论如何都可以轻松地以编程方式设置+取消设置文件权限。
我有一个类似的问题 - 想测试权限问题+想出了以下帮助程序类来包装库 API 以搞乱文件权限
为所有用户设置 c:\program files\company\app\file 的文件权限
I'd go for a proper acceptance test - using mocks too much can be a bit dangerous. In this case it's easy to programmatically set + unset file permissions anyway.
I had a similar problem - wanted to test a permissions problem + came up with the following helper class to wrap the library API for messing around with file permissions
set file permissions for c:\program files\company\app\file for all users