-2

谁能帮我这个?

 [TestMethod]
    public void MakeDateConstructsADateTimeFromYearMonthAndDay()
    {
        Arrange
        var controller = new DateController();
        var expected = new DateTime(2014, 6, 30);

        Act
        var result = controller.MakeDate(2014, 6, 30);

        Assert
        Assert.AreEqual<DateTime>(expected, result);
    }

    [TestMethod]
    public void MakeDateReturnsDefaultDateTimeIfInputDataInvalid()
    {
        /* HINT You need to use a try ... catch block in the MakeDate()
         * method to trap the exception thrown by the DateTime constructor.
         * See http://msdn.microsoft.com/en-us/library/xcfzdy4x(v=vs.110)/ 
         * for information on the exceptions thrown by the DateTime 
         * constructor.  
         * See http://msdn.microsoft.com/en-us/library/ms173160(v=vs.110).aspx 
         * for information on exception handling in C#.  
         * */
        //Arrange
        var controller = new DateController();//error
        var expected = new DateTime();//error

        //Act
        //June has only 30 days so this will cause an exception
        var result = controller.MakeDate(2014, 6, 31);

        //Assert
        Assert.AreEqual<DateTime>(expected, result);
    }

不断收到错误说明 Error Expected class, delegate, enum, interface, or struct //getting errors under 'void, datacontroller and datetime

4

2 回答 2

1
Assert.AreEqual<DateTime>(expected, result);

那行代码断言它的两个参数是相等的。这是一个通用方法,所以这个<DatTime>位告诉方法两个参数的类型是什么。就像是:

bool SomeGenericCompareMethod<T>( T arg1, T arg2 )
{
    // use reference equality by default, 
    // implementation may override .Equals()
    return arg1.Equals( arg2 );
}

编辑:

根据您在评论中完全未格式化和乱码的代码,这到底是什么?

Arrange var controller = new DateController();//Getting Error

这是没有意义的。您似乎有两个类型声明符,Arrange并且var. 选一个。我会选择这个,因为不清楚这Arrange是一个DateController派生自的基类/接口:

var controller = new DateController();//Getting Error
于 2012-11-08T21:03:15.113 回答
0

断言可以用作应用程序中的运行时检查(即您提到的方法比较两个参数并使用相等运算符验证它们是否相等)。

如果一个断言计算为true,它只是让代码继续,但如果它计算为falseAssertFailedException则被抛出。

有关 msdn 的一些信息,请查看:http: //msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert%28v=vs.80%29.aspx

于 2012-11-08T21:08:56.890 回答