NStub.CSharp.NamespaceDetector.PrepareNamespaceImports C# (CSharp) Method

PrepareNamespaceImports() public method

Prepares the specified namespace imports to prevent collisions with the types under test.
This method detects problematic namespaces like in the example below and prefixes them with global:: to prevent namespace collisions in the test unit files.

The namespace of the object to test is so the unit test becomes:

Take a look at the 'using MbUnit.Framework' import. In that way it conflicts with the 'NStub.CSharp.MbUnit' import and circumvents the name resolution of the TestAttribute. This method detects such concerns and puts a global:: prefix in front of the 'MbUnit.Framework' namespace import.

public PrepareNamespaceImports ( IEnumerable imports ) : IEnumerable
imports IEnumerable The namespace imports.
return IEnumerable
        public IEnumerable<CodeNamespaceImport> PrepareNamespaceImports(IEnumerable<string> imports)
        {
            var result = new List<CodeNamespaceImport>();
            foreach (var ns in imports)
            {
                var corectedns = this.PrepareNamespace(ns);
                result.Add(new CodeNamespaceImport(corectedns));
            }

            return result;
        }

Usage Example

Exemplo n.º 1
0
        public void PrepareNamespaceImportsTest()
        {
            var imports = new[] { "System","System.Linq", "MbUnit.Framework" };
            var expected = ".Tests.SubNamespace";
            var actual = testObject.PrepareNamespaceImports(imports);
            Assert.AreElementsEqual(imports, actual.Select(e => e.Namespace));

            var expectedPrepared = new[] { "System", "System.Linq", "global::MbUnit.Framework" };
            typeDeclarations.Add(new CodeTypeDeclaration("Jedzia.Loves.Testing.MbUnit.SubClass"));
            this.testObject = new NamespaceDetector(this.typeDeclarations);
            actual = testObject.PrepareNamespaceImports(imports);
            Assert.AreElementsEqual(expectedPrepared, actual.Select(e => e.Namespace));
        }