SpecsFor.Mvc中的URL更改

时间:2015-02-03 22:03:13

标签: c# asp.net-mvc-5 integration-testing

假设您像这样配置SpecsForIntegrationHost

config.UseApplicationAtUrl("http://mylocaldomain.com");

您的某些网页位于子域中,因为您在RouteConfig中以此方式进行了配置。您无法测试这些,因为您需要更改主机。

public class When_Viewing_Global_Page : SpecsFor<MvcWebApp>
{
    protected override void When()
    {
        //The HomeController.Global is triggered only
        //in the URL http://global.mylocaldomain.com
        //this results in 404
        SUT.NavigateTo<HomeController>(c => c.Global());
    }

    [Test]
    public void Then_It_Shows_The_Project_Name()
    {
        string text = SUT.AllText();
        SUT.AllText().ShouldContain("This is the Global Page");
        //This will fail because the page contains "Not Found"
    }
}

有没有办法告诉SpecsFor.Mvc中的测试更改基本URL?

1 个答案:

答案 0 :(得分:1)

原来有一个公共静态属性,它保存了基本网址名称。您可以简单地更改它,但如果您的测试未分类,则必须将其更改回来。

public class When_Viewing_Global_Page : SpecsFor<MvcWebApp>
{
    protected override void When()
    {
        //change to base url to the subdomain
        MvcWebApp.BaseUrl = "http://global.mylocaldomain.com";
        SUT.NavigateTo<HomeController>(c => c.Global());
    }

    [Test]
    public void Then_It_Shows_The_Project_Name()
    {
        string text = SUT.AllText();
        SUT.AllText().ShouldContain("This is the Global Page");
        //Success
    }

    [TestFixtureTearDown]
    public void Cleanup()
    {
        //you have to change that back to the URL that was
        //set up in your SpecsForIntegrationHost
        MvcWebApp.BaseUrl = "http://mylocaldomain.com";
    }
}

这应该很容易在父类中抽象出来。