1

I would like to test a method with optional Parameters in MSTest.

private CalcSomthing(double valueone, double valuetwo = 10) 
{ 
   // Do somthing 
}

When i call this method it works fine. But wen i run it in Unittest (MSTest) the Valuetwo wouldn't initialize with value 10.

Are MSTest unable to test optional Parameter or i'm wrong?

    [TestMethod]
    public void CalcSomthingTest()
    {
        var someclass= new Someclass_Accessor();
        someclass.CalcSomthing(10);
    }

The result is: Valueone = 10 and ValueTwo = 0.0;

San
  • 868
  • 1
  • 9
  • 18

2 Answers2

0

This test passes in Visual Studio 2010:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestProject1
{
    public class Someclass
    {
        public double CalcSomthing(double valueone, double valuetwo = 10)
        {
            Assert.IsTrue(valuetwo == 10);
            return valueone + valuetwo;
        }
    }

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void CalcSomthingTest()
        {
            var someclass = new Someclass();
            someclass.CalcSomthing(10);
        } 
    }
}

in either ReSharper or TestRunner:

------ Test started: Assembly: TestProject1.dll ------

1 passed, 0 failed, 0 skipped, took 1.14 seconds (MSTest 10.0).

Peter K.
  • 8,028
  • 4
  • 48
  • 73
  • I've tested your solution in a TestProjekt and it works. The Problem in my Projekt is, that I use SomeClass_Accessor and then it doesn't work. – San Mar 08 '11 at 12:28
  • I'm not sure what you mean by *SomeClass_Accessor*. Can you update your example with something compilable (and testable) that fails? I wonder if your failing example has the compiler doing optimizations because the parameter is not used by the code? – Peter K. Mar 08 '11 at 17:56
  • I know, long time ago. I'd like to test private methodes and for that i need the Accessor. As soon i use an Accessor it doesn't work anymore. – San Oct 18 '11 at 12:52
  • Is it possible to change the "private" to "internal" ? That way you can use the attribute [InternalsVisibleTo()](http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx). – Peter K. Oct 18 '11 at 13:01
  • The idea is that no other has access to this method. The Question is: Do I need to test private methods? – San Oct 18 '11 at 13:11
  • If they are private, then they probably should not be testable. [Many people think](http://stackoverflow.com/questions/105007/do-you-test-private-method) that testing private methods makes the tests too fragile, as they depend upon the implementation details. – Peter K. Oct 18 '11 at 13:20
0

Agreed its a problem.

Make the method above private and then SomeClass_Accessor is generated, the read-only metadata for which exposes the signature

public double CalcSomthing(double valueone, double valuetwo = null)

The code works fine but the test fails

Any solutions ?

Test
  • 1
  • My Solution is not uses optional parameters. I had search for many opinions and all recommandet to make overloadet methodes. – San Oct 18 '11 at 12:48