是否可以创建NUnit Test方法来检查方法是否返回预期的数据类型?
这就是我的意思:
我有一个静态字符串,它接受两个参数并检查它是否与另一个字符串匹配.如果是,方法只返回该字符串.
我想测试以确保此方法确实返回字符串类型和可能发生的任何异常.
示例代码:
public static string GetXmlAttributeValue(this XmlElement element, string attributeName)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
if (attributeName == null)
{
throw new ArgumentNullException("attributeName");
}
string attributeValue = string.Empty;
if (element.HasAttribute(attributeName))
attributeValue = element.Attributes[attributeName].Value;
else
throw new XmlException(element.LocalName + " does not have an attribute called " + attributeName);
return attributeValue;
}
Run Code Online (Sandbox Code Playgroud)
以下是我的解决方案的样子:
我想在TestLibrary类库中编写测试代码.
下面的代码片段是我的滑块jQuery,但它停在最后一张图片上,而不是再循环到第一张图片.有任何想法吗?
function cycleImages() {
var $active = $('#cycler .active');
var $next = ($active.next().length>0) ? $active.next():$('cycler img:first');
$next.css('z-index', 2);
$active.fadeOut(1500, function () {
$active.css('z-index', 1).show().removeClass('active');
$next.css('z-index', 5).addClass('active');
});
}
$(document).ready(function () {
setInterval('cycleImages()', 4000);
})
Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数来测试字符串是否是回文,我想知道如果字符串确实是回文,如何返回中间的一个或多个字母?
这是我到目前为止所拥有的:
我的 bool 检查字符串是否为回文:
public static bool IsPalindrome(string input)
{
int i = 0;
int j = input.Length - 1;
while (true)
{
if (i > j)
{
return true;
}
char a = input[i];
char b = input[j];
if (!a.Equals(b))
{
return false;
}
i++;
j--;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我希望能够打印出中间字母的地方:
while (true)
{
Console.Clear();
Regex myRegex = new Regex("[ ;:,.-?'!\"]");
string userInput = String.Empty;
Console.WriteLine("Please enter sentence or phrase");
userInput = Console.ReadLine();
Console.WriteLine();
if (IsPalindrome(myRegex.Replace(userInput, string.Empty).ToLower()))
{
Console.WriteLine("True");
Console.WriteLine("Press …Run Code Online (Sandbox Code Playgroud)