简体   繁体   中英

Two projects in a solution that use different default paths

I have two projects in my solution, ChatProject and ChatProjectTest .

ChatProjectTest has a single class file that has many methods that test methods in ChatProject .

My problem is that ChatProject uses some files, like data.bin and log.txt , that are stored in the .exe folder (that is ChatProject\\bin\\Debug\\ChatProject.exe ). I want ChatProjectTest to use the same files, and not go ahead and create or load new files in its own .exe folder ( ChatProjectTest\\bin\\Debug\\ChatProject.exe ), like it does currently.

To be clear, the file paths are stored in constant variables in ChatProject like this:

private static string DATABASE_NAME = "data.bin";

I didn't hardcode their paths.

One solution can be configuring your projects to put all the output files into the same the same bin/ directory under your solution root (instead of two separate bin/ folders). To do this, go to the "Build" tab under project settings and set "Output path" to

$(SolutionDir)Bin\\$(Configuration)\\

Repeat this for both of your projects.

If you don't specify a path to a file it looks in the current directory for them. To use the same ones, you either have to specify some sort of relative path from the Test Output, or build the tests into the same folder as the Project.

You may allow the target file path to be injected eg via an optional constructor parameter:

public class SampleClass
{
    private readonly string OutputDirectory;

    public SampleClass(string outputDirectory = ".")
    {
        OutputDirectory = outputDirectory;
    }

    public void WriteData(string data)
    {
        File.AppendAllText($"{OutputDirectory}\\data.bin", data);
    }
}

[TestClass]
public class SampleClassTest
{
    [TestMethod]
    public void WriteDataTest()
    {
        var sut = new SampleClass("..\\..\\..\\ChatProject\\bin\\Debug");
        sut.WriteData("data");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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