0

I have got a test to assert then when my application generates an application number(RHI), it asserts that the generated number on the final page is equal to the number in the db;

public void AssertRHINumberFromDB()
{
    string rhiFromText = GenerateRHINumberIntoTextFile();
    string query = "Select RHINumber from RHI.Application where RHINUMBER = 'rhiFromText'";
    var rhiFromDB = Helper.ExecuteScalar(query);

    Assert.Equals(rhiFromDB.Contains("RHINUMBER"), rhiFromText);
}

Though, when the ExecuteScalar method goes, I am hit with Object Reference Error and rhiFromDB is returning null.

relevant code:

public string GenerateRHINumberIntoTextFile()
{
    var rhiPath = AppDriver.Driver.FindElement(By.CssSelector("#main-content>div>p:nth-child(2)>b"));
    string generatedRHI = rhiPath.GetAttribute("innerText");
    Helper.DeleteTextFilesAfterThreeMinutes();
    Helper.WriteTextFile("TestFile1", generatedRHI);

    return generatedRHI;
}

public static string ExecuteScalar(string command, SqlParameter[] parms = null)
{
    using (ISQLService sql = new SQLService(new SqlConnection(ConfigurationManager.AppSettings["DBConnection"])))
    {
        return sql.ExecuteScalar(command, parms);
    }
}

Any pointers?

Milo
  • 3,365
  • 9
  • 30
  • 44
Cbear
  • 17
  • 5
  • It sounds like the database query isn't finding anything. I'm not sure what exactly you're asking, I guess it's up to you to define how you want your logic to handle that scenario. If that scenario shouldn't happen and it should always find a record then check your database and see if that record is there. – David Mar 16 '20 at 13:49
  • Also, what does this even mean?: `Assert.Equals(rhiFromDB.Contains("RHINUMBER"), rhiFromText);` You're trying to compare a boolean with a string. – David Mar 16 '20 at 13:54
  • right, haven't fixed that bit because I've been focused on getting it past the executescalar part – Cbear Mar 16 '20 at 13:58
  • 1
    According to documentation (https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executescalar?view=netframework-4.8) `ExecuteScalar` returns `null` when no record is found. So you'll either need to ensure that the record is there before running this code or handle `null` in the code. – David Mar 16 '20 at 14:01

0 Answers0