简体   繁体   English

如何在NUnit中单元测试文件权限?

[英]How to unit test file permissions in 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 PS:我正在使用Rhino mock和NUnit

You could use Rhino.Mocks "Do" extension to throw a specific exception: 你可以使用Rhino.Mocks“Do”扩展来抛出一个特定的异常:

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. 这将允许您测试您的异常处理代码。

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. 如果你需要一个更好的答案,你需要举一个你的课程的例子,也许你到目前为止所写的测试的骨架。

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 我有一个类似的问题 - 想测试一个权限问题+想出了以下帮助器类来包装库API以搞乱文件权限

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM