mardi 29 septembre 2015

How to use a resource file in Visual Studio 2015

I'm creating integration tests (web class library project) for my ASP5 MVC6 application. I want to add a resource file with raw SQL to the test project for DB preparation.

I wanted to use good old resources (.resx). But I it's not available in add new item project's menu.

I found this answer pointing to a github repo. I guess this is where he reads the file:

using (var stream = FileProvider.GetFileInfo("compiler/resources/layout.html").CreateReadStream())
                using (var streamReader = new StreamReader(stream))
                {
                    return streamReader.ReadToEnd().Replace("~", $"{basePath}/compiler/resources");
                }

I tried using System.IO.File.ReadAllText("compiler/resources/my.sql") in my test helper project in TestHelper class.

When I used the TestHelper class in my actual test project it was looking for the the file in test project directory.

TestProject/compiler/resources/my.sql insetad of TestHelperProject/compiler/resources/my.sql

I can figure out a couple of workarounds. However I'd like to do it the right way. Preferably like I would do in with a resx file:

string sql = Resources.MySql;

Any suggestions?

Aucun commentaire:

Enregistrer un commentaire