Вопрос

Trying to follow this: http://msdn.microsoft.com/en-us/library/21a15yht.aspx The problem with the example is it's a mixture of instructions for visual studio users and command line users, but I have done my best to follow only the VS instructions.

So I have: Created a new Unit Test Project in visual studio called Example.

I added a resource called Greeting.en-US.resx and put a string in called HelloString.

Edit I have now added another default resource file called Greeting.resx string is "Hello (Default)"

enter image description here

My unit test:

using System.Globalization;
using System.Resources;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Example
{
    [TestClass]
    public class GreetingTests
    {
        [TestMethod]
        public void Hello()
        {
            var newCulture = new CultureInfo("en-US");
            Thread.CurrentThread.CurrentCulture = newCulture;
            Thread.CurrentThread.CurrentUICulture = newCulture;

            var rm = new ResourceManager("Example.Greeting",
                                         typeof (GreetingTests).Assembly);
            Assert.AreEqual("Hello (US)", rm.GetString("HelloString"));
        }
    }
}

Now it only ever loads the default

Assert.AreEqual failed. Expected:<Hello (US)>. Actual:<Hello (Default)>.

The same code works in a Console App just fine.

Это было полезно?

Решение

Well, I think this happens because your unit testing tool shadow copies main assembly but does not copy the satellite one. Enable shadow copying for satellite assembly and rerun the unit test.

To confirm my guess check value of Assembly.GetExecutingAssembly().Location against .Codebase.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top