mercredi 28 octobre 2015

How do I test my c#-based cmdlet?

I've been looking all over the place for a decent example and most results are basics of power shell....

But anyway, I did manage to find this: http://ift.tt/1iiSvkt

Which gave me a start, and allowed me to end up with this test, based of the example in the above link:

using System;
using System.Collections.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using MyProject;

namespace Commands.Tests
{
    [TestClass]
    public class StartServiceTest
    {
        private RunspaceConfiguration config;

        [TestInitialize]
        public void Initialize()
        {
            config = RunspaceConfiguration.Create();
            config.Cmdlets.Append(new CmdletConfigurationEntry(
                "Start-UseService",
                typeof(StartUseServiceCommand),
                "Microsoft.Windows.Installer.PowerShell.dll-Help.xml"));
        }

        [TestMethod]
        [DeploymentItem(@"data\example.txt")]
        public void PathTest()
        {
            using (Runspace rs = RunspaceFactory.CreateRunspace(config))
            {
                rs.Open();
                using (Pipeline p = rs.CreatePipeline(@"start-useservice -service ACE"))
                {
                    Collection<PSObject> objs = p.Invoke();
                    Assert.AreEqual<int>(1, objs.Count);
                }
            }
        }
    }
}

The article doesn't explain anything, so I'm sure "data\example.txt" doesn't apply to me, but example.txt is used as a parameter in the example's test. But the method attribute just isn't discussed.

Has anyone successfully wrote a test for their cmdlet? I'm also looking for a bit of an explanation of what each piece does. (I'm not new to MSTest, but I've never used the System.Management.Automation/Runspaces Namespace(s) before)

My cmdlet name is Start-UseService

Aucun commentaire:

Enregistrer un commentaire