URL SpecsFor.Mvc 的变化

URL change in SpecsFor.Mvc

假设您像这样配置 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?

原来有一个 public 静态 属性 保存基本 url 名称。您可以简单地更改它,但如果您的测试未排序,则必须将其更改回来。

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";
    }
}

虽然这应该很容易在父级中抽象出来class。