简体   繁体   English

我如何在c#中对单元测试的URL有效?

[英]How do I Unit test is URL valid in c#?

I have a class that checks to see if the URL is valid using the method below: 我有一个类,使用以下方法检查URL是否有效:

return !string.IsNullOrEmpty(url) && 
        Uri.TryCreate(url, UriKind.Absolute, out uriResult) && 
        (uriResult.Scheme == Uri.UriSchemeHttp || 
        uriResult.Scheme == Uri.UriSchemeHttps);

I was wondering how I would unit test the code to check to see if the URL is valid or not. 我想知道如何对代码进行单元测试,以检查URL是否有效。

So far, i have a unit test that only partially covers the unit test which is below. 到目前为止,我的单元测试仅部分涵盖了下面的单元测试。

string url = "newUrl";
var result = UrlUtility.IsValid(url);
Assert.IsFalse(result);

Any help would be much appreciated. 任何帮助将非常感激。 Thanks 谢谢

You should cover all conditions of IsValid() method: 您应该涵盖IsValid()方法的所有条件:

[TestMethod]
public void TestEmptyUrl()
{
    string url = "";

    Assert.IsFalse(UrlUtility.IsValid(url));
}

[TestMethod]
public void TestInvalidUrl()
{
    string url = "bad url";

    Assert.IsFalse(UrlUtility.IsValid(url));
}

[TestMethod]
public void TestRelativeUrl()
{
    string url = "/api/Test";

    Assert.IsFalse(UrlUtility.IsValid(url));
}

[TestMethod]
public void TestUrlWithInvalidScheme()
{
    string url = "htt://localhost:4000/api/Test";

    Assert.IsFalse(UrlUtility.IsValid(url));
}

[TestMethod]
public void TestUrlWithHttpScheme()
{
    string url = "http://localhost:4000/api/Test";

    Assert.IsTrue(UrlUtility.IsValid(url));
}

[TestMethod]
public void TestUrlWithHttpsScheme()
{
    string url = "https://localhost:4000/api/Test";

    Assert.IsTrue(UrlUtility.IsValid(url));
}

You can give some values for the url. 您可以为网址提供一些值。 For example you can test the next case: 例如,您可以测试下一个案例:

string url = string.Empty;
var result = UrlUtility.IsValid(url);
Assert.IsFalse(result);

Also, you can read about the Uri.UriSchemeHttp and Uri.UriSchemeHttps on the internet and try to make some test to respect or to ignore this uri scheme. 此外,您可以在互联网上阅读有关Uri.UriSchemeHttpUri.UriSchemeHttps的信息,并尝试进行一些测试以尊重或忽略此uri方案。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为C#Azure功能编写单元测试? - How do I write Unit Test for C# Azure Function? 如何正确使用 C# 单元测试 FakeHttpContext? - How do I use C# Unit test FakeHttpContext correctly? 如何使用 Ms unit project c# 代码为逻辑应用程序步骤编写单元测试? - How do I write unit test for logic apps steps using Ms unit project c# code? 如何在 C# 控制台应用程序的单元测试中测试一般异常? - How do I test a general exception in a unit test of my C# Console Application? 如何重构此C#SQL查询以进行单元测试? - How do I refactor this C# SQL query to do a unit test? 如何在 C# 中进行单元测试 - How to unit test in C# 如何配置Visual Studio C#单元测试解决方案以接受不同的运行时配置? - How do I configure a Visual Studio C# unit test solution to accept different runtime configurations? 如何对返回FUNC的C#函数进行单元测试 <something> ? - How do I unit test a C# function which returns a Func<something>? 如何向C#应用程序添加MSTest单元测试支持 - How do I add a MSTest unit test support to a C# application 如何在 C# 单元测试中伪造内置方法的返回? - How do I fake the return of a built-in method in C# unit test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM