paradise_human
paradise_human

Reputation: 695

Is there a way to unit test localization resources in asp.net core class libarary?

I want to have a resource class library and test it in another mstest class libary. this is my project.json in resource library : (my resx file is in Resources folder)

{
  "version": "1.0.0-*",
  "dependencies": {
  "NETStandard.Library": "1.6.0",
  "Microsoft.AspNetCore.Mvc.DataAnnotations": "1.0.1"
 },
 "buildOptions": {
  "embed": {
    "include": [ "Resources/PS.ModelBase.Models.Person.fa.resx" ]
  }
 },
 "frameworks": {
  "netstandard1.6": {
   "imports": "dnxcore50"
  }
}
  }

and this is my test method :

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Resources;
using System.Threading.Tasks;

namespace PS.ModelBaseTest
{
[TestClass]
public class PersonTest
{
    [TestMethod]
    public void FullNameValidateNullFirstName()
    {
        ResourceManager rm = new ResourceManager(typeof(PS.ModelBase.Models.Person));
        string test = rm.GetString("Fisrt Name", new System.Globalization.CultureInfo("fa-IR"));
        Assert.AreEqual("Fisrt Name", test);

    }
  }
 }

I have this error when I run the test :

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "PS.ModelBase.Models.Person.resources" was correctly embedded or linked into assembly "PS.ModelBase" at compile time, or that all the satellite assemblies required are loadable and fully signed.

what should I do ?

َUpdate :

Ok. I solved my problem by inserting resx to the Models folder (Where the Person class is in it ) and renamed resx files to class name (like Person.en.resx) . Nothing to do with project.json :

{
   "dependencies": {
   "NETStandard.Library": "1.6.0",
    "Microsoft.AspNetCore.Mvc.DataAnnotations": "1.0.1"
},
    "frameworks": {
    "netstandard1.6": {
     "imports": "dnxcore50"
 }
 },
 "version": "1.0.0-*"
}

But I faced another problem .I want to remain my resx files in Resources folder , not in Models folder! How can embed resx file to person class when is in Resources folder ?

I got the second problem. In fact the resource file and the class must be in a same domain

Upvotes: 4

Views: 2284

Answers (1)

Nkosi
Nkosi

Reputation: 247018

Using the following two resources as reference :

Github Issue: Updates to project.json schema

project.json reference

Based on project file in question, either try

  • "includeFiles" string/string[]
    A list of file paths to include. The paths are rooted at the project folder. This list has a higher priority than the include and exclude globbing patterns, hence a file listed here and in the exclude globbing pattern will still be included. Defaults to none.
"buildOptions": {
  "embed": {
    "includeFiles": [ "Resources/PS.ModelBase.Models.Person.fa.resx" ]
  }
}

for just the one resource file, or

  • "include" string/string[]
    A list of file globbing patterns for files to include. The patterns are rooted at the project folder. Defaults to none.
"buildOptions": {
  "embed": {
    "include": [ "Resources" ]
  }
}

for the contents of the Resources folder

Upvotes: 1

Related Questions