简体   繁体   English

编写单元测试时出现System.UnauthorizedAccessException

[英]System.UnauthorizedAccessException while writing a unit test

Hello I am building a chatroom system and was asked to write a unit test to it with Nunit. 您好,我正在构建一个聊天室系统,并被要求使用Nunit对其进行单元测试。 The "UserData" is currently stored in a .bin file. “ UserData”当前存储在.bin文件中。 I wrote a test which registers a user and tries to login with its credentials. 我编写了一个注册用户并尝试使用其凭据登录的测试。 For some reason the test fails and throws an Exception saying that the access to the file is denied. 由于某种原因,测试失败,并引发异常,表明对该文件的访问被拒绝。

Here is the test i wrote : 这是我写的测试:

        [Test]
    public void Login_Registered_User_Test() {

        ChatRoom cr = new ChatRoom();
        cr.Start();

        String username = "test123";
        String groupId = "5";


        Assert.AreEqual(true,cr.Register(username, groupId));
        Assert.AreEqual(true,cr.Login(username, groupId));

    }

and the exception is coming from here : 和例外是从这里来的:

        private void createFile() {

        Stream stream = new FileStream(FolderName, FileMode.Create);
        stream.Close();

    }

Message: System.UnauthorizedAccessException : Access to the path 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\IDE\\UserData.bin' is denied. 消息:System.UnauthorizedAccessException:拒绝访问路径“ C:\\ Program Files(x86)\\ Microsoft Visual Studio \\ 2017 \\ Community \\ Common7 \\ IDE \\ UserData.bin”。

i think the visual studio is not able to access the Programfiles folder. 我认为Visual Studio无法访问Programfiles文件夹。 Try opening the VS in Administrator mode and run the unit test . 尝试以管理员模式打开VS并运行单元测试。

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

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