Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 仅在SpecFlow测试期间重定向时TempData为空_Asp.net Mvc_Testing_Specflow_Razor Pages - Fatal编程技术网

Asp.net mvc 仅在SpecFlow测试期间重定向时TempData为空

Asp.net mvc 仅在SpecFlow测试期间重定向时TempData为空,asp.net-mvc,testing,specflow,razor-pages,Asp.net Mvc,Testing,Specflow,Razor Pages,我正在使用SpecFlow、Selenium和ChromeDriver测试一个.NETCore2.2Web应用程序,我被我遇到的一些事情弄糊涂了 我使用TempData在发布数据的页面和显示数据的页面之间传递警报消息的数据。在本地部署到IIS进行调试等时,我会看到TempData,直到它被使用并以格式化消息的形式显示在页面上。但是,我在SpecFlow场景中有一个步骤,当由于TempData为空而未显示警报时失败。SpecFlow场景遵循手动测试期间采取的相同步骤 我正在从一个引用的XUnit/

我正在使用SpecFlow、Selenium和ChromeDriver测试一个.NETCore2.2Web应用程序,我被我遇到的一些事情弄糊涂了

我使用TempData在发布数据的页面和显示数据的页面之间传递警报消息的数据。在本地部署到IIS进行调试等时,我会看到TempData,直到它被使用并以格式化消息的形式显示在页面上。但是,我在SpecFlow场景中有一个步骤,当由于TempData为空而未显示警报时失败。SpecFlow场景遵循手动测试期间采取的相同步骤

我正在从一个引用的XUnit/Specflow测试项目测试一个.NETCore2.2Web项目。使用以下相关依赖项:

  • Microsoft.NetCore.App v2.2.0
  • SpecFlow v3.0.225
  • SpecFlow.xUnit v3.0.225
  • Selenium.WebDriver v3.141.0
  • Selenium.WebDriver.ChromeDriver v76.0.3809.12600
我已经确认,在本地调试应用程序时,TempData按预期填充了消息数据。调试测试表明,TempData在表单数据成功过帐时填充,但在重定向期间处理生成的OnGet时为空

以下是用于添加帐户的post处理程序:

public async Task<IActionResult> OnPostAsync()
{
    if (ModelState.IsValid)
    {
        var user = new IdentityUser { UserName = Account.UserName, Email = Account.EmailAddress };
        var result = await _userManager.CreateAsync(user);

        if (result.Succeeded)
        {

            Dictionary<string, string> SuccessAlert = new Dictionary<string, string>()
            {
                { "Type", "success" },
                { "Title", "Account Added" },
                { "Text",  "Account for user " + Account.UserName + " was added successfully."}
            };

            TempData["Alert"] = SuccessAlert;

            _logger.LogInformation($"account added successfully. Alert title: {SuccessAlert["Title"]}");

            return RedirectToPage("/Accounts/Index", new
            {
                area = "Administration"
            });
        }

        foreach (var error in result.Errors)
        {
            ModelState.AddModelError("", error.Description);
        }

    }

    Dictionary<string, string> ErrorAlert = new Dictionary<string, string>()
            {
                { "Type", "error" },
                { "Title", "Uh-oh!" },
                { "Text",  "Account for user " + Account.UserName + " was not added. See errors below."}
            };

    TempData["Alert"] = ErrorAlert;

    return Page();
}
2019-08-28 16:18:53.917 -04:00 First.Web.Areas.Administration.Accounts.AddAccountModel [Information] account added successfully. Alert title: Account Added
2019-08-28 16:19:08.587 -04:00 First.Web.Areas.Administration.Accounts.AccountsIndexModel [Information] AccountsIndexModel.OnGet
2019-08-28 16:19:08.588 -04:00 First.Web.Areas.Administration.Accounts.AccountsIndexModel [Information] Items in TempData: 1
以下是与上述代码中的日志记录相对应的日志文件片段

手动添加帐户的日志:

public async Task<IActionResult> OnPostAsync()
{
    if (ModelState.IsValid)
    {
        var user = new IdentityUser { UserName = Account.UserName, Email = Account.EmailAddress };
        var result = await _userManager.CreateAsync(user);

        if (result.Succeeded)
        {

            Dictionary<string, string> SuccessAlert = new Dictionary<string, string>()
            {
                { "Type", "success" },
                { "Title", "Account Added" },
                { "Text",  "Account for user " + Account.UserName + " was added successfully."}
            };

            TempData["Alert"] = SuccessAlert;

            _logger.LogInformation($"account added successfully. Alert title: {SuccessAlert["Title"]}");

            return RedirectToPage("/Accounts/Index", new
            {
                area = "Administration"
            });
        }

        foreach (var error in result.Errors)
        {
            ModelState.AddModelError("", error.Description);
        }

    }

    Dictionary<string, string> ErrorAlert = new Dictionary<string, string>()
            {
                { "Type", "error" },
                { "Title", "Uh-oh!" },
                { "Text",  "Account for user " + Account.UserName + " was not added. See errors below."}
            };

    TempData["Alert"] = ErrorAlert;

    return Page();
}
2019-08-28 16:18:53.917 -04:00 First.Web.Areas.Administration.Accounts.AddAccountModel [Information] account added successfully. Alert title: Account Added
2019-08-28 16:19:08.587 -04:00 First.Web.Areas.Administration.Accounts.AccountsIndexModel [Information] AccountsIndexModel.OnGet
2019-08-28 16:19:08.588 -04:00 First.Web.Areas.Administration.Accounts.AccountsIndexModel [Information] Items in TempData: 1
对于SpecFlow自动测试:

2019-08-28 17:19:02.014 -04:00 First.Web.Areas.Administration.Accounts.AddAccountModel [Information] account added successfully. Alert title: Account Added
2019-08-28 17:19:02.105 -04:00 First.Web.Areas.Administration.Accounts.AccountsIndexModel [Information] AccountsIndexModel.OnGet
2019-08-28 17:19:02.105 -04:00 First.Web.Areas.Administration.Accounts.AccountsIndexModel [Information] Items in TempData: 0
以下是SpecFlow场景:

Scenario: Add new account succeeds
    Given I have access to the "Administration" module
    When I navigate to the "Add Account" page
        And I submit an account with values
            | accountname | emailaddress         |
            | mcullen     | mcullen@mnhockey.com |
    Then I should be on the "Accounts" page
        And I should see a success message with
            | title         | description      |
            | Account Added | Account for user |
以及帐户提交步骤的步骤定义:

[When(@"I submit an account with values")]
public void WhenISubmitAnAccountWithValues(Table table)
{
    var accountName = "";
    var emailAddress = "";

    var row = table.Rows[0];

    row.TryGetValue("accountname", out accountName);
    row.TryGetValue("emailaddress", out emailAddress);

    _accountsAddPage.AccountNameField.SendKeys(accountName);
    _accountsAddPage.EmailAddressField.SendKeys(emailAddress);

    _accountsIndexPage = _accountsAddPage.Submit();

    _context.Set<string>("Accounts", "currentpage");
}
[当(@“我提交一个有值的帐户”)]
当submitanaccountwithvalues(表)时公共无效
{
var accountName=“”;
var emailAddress=“”;
var row=表。行[0];
行.TryGetValue(“accountname”,out accountname);
row.TryGetValue(“emailaddress”,out emailaddress);
_accountsAddPage.AccountNameField.SendKeys(accountName);
_accountsAddPage.EmailAddressField.SendKeys(emailAddress);
_accountsIndexPage=_AccountsAdPage.Submit();
_设置(“账户”、“当前页面”);
}

在对我的问题进行更多故障排除时,我发现ChromeDriver打开的浏览器没有被发送到响应头中的.AspNetCore.Mvc.CookieTempDataProvider cookie

为了解决这个问题,我在Startup.cs中添加了以下配置:

services.Configure<CookieTempDataProviderOptions>(options =>
{
    options.Cookie.IsEssential = true;
});
services.Configure(选项=>
{
options.Cookie.IsEssential=true;
});
或者,您也可以省略上述代码,并将非必要cookie所需的检查代码更改为false:

services.Configure<CookiePolicyOptions>(options =>
{
    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
    options.CheckConsentNeeded = context => false;
    options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.Configure(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>false;
options.MinimumSameSitePolicy=SameSiteMode.None;
});

这是基于找到的文档。因为我的项目是针对intranet站点的,所以我不使用CookiePolicyMiddleware,上面的解决方案是合适的。

您确定
\u accountsAdPage.Submit()
方法正在提交您认为是的页面吗?您好,Greg。这是因为我调试了测试以查看它,并且数据库反映了提交的新数据。