C
C#13mo ago
hugheselite

✅ Help with unit testing a save function on a simple windows form app

For some reason my unit test for my save function isn't writing any text to a text file (to ensure the text actually has been saved) i'm getting the error string.empty when trying to execute the Nunit Test
13 Replies
hugheselite
hugheseliteOP13mo ago
else if (CommandSplit[0].Equals("save"))
{
string path = @"C:\Users\Hughes Portable Rig\source\repos\GraphicalProgrammingEnvironment";
string fileName = "saved_user_input.txt";

try
{
// Combine the path and file name
string filePath = Path.Combine(path, fileName);

// Save the contents of SingleCommandLine to the specified file
File.WriteAllText(filePath, SingleCommandLineSaved);

ErrorHandlingWindow.Clear();
SingleCommandLine.Clear();
ErrorHandlingWindow.Text = $"** File '{fileName}' has been saved at '{filePath}' **";
}
catch (Exception ex)
{
ErrorHandlingWindow.Text = $"Error while saving: {ex.Message}";
}
}
else if (CommandSplit[0].Equals("save"))
{
string path = @"C:\Users\Hughes Portable Rig\source\repos\GraphicalProgrammingEnvironment";
string fileName = "saved_user_input.txt";

try
{
// Combine the path and file name
string filePath = Path.Combine(path, fileName);

// Save the contents of SingleCommandLine to the specified file
File.WriteAllText(filePath, SingleCommandLineSaved);

ErrorHandlingWindow.Clear();
SingleCommandLine.Clear();
ErrorHandlingWindow.Text = $"** File '{fileName}' has been saved at '{filePath}' **";
}
catch (Exception ex)
{
ErrorHandlingWindow.Text = $"Error while saving: {ex.Message}";
}
}
This is my save function im trying to test
[Test]
public void TestSave()
{
// Arrange
var mainForm = new mainForm();
var filePath = @"C:\Users\Hughes Portable Rig\source\repos\GraphicalProgrammingEnvironment\saved_user_input.txt";
var content = "test content";
mainForm.SingleCommandLine.Text = $"save {filePath} \"{content}\"";

// Act
mainForm.ExecuteCommand();

// Assert
Assert.IsTrue(File.Exists(filePath));
Assert.AreEqual(content, File.ReadAllText(filePath));

// Clean up
File.Delete(filePath);
}
}
[Test]
public void TestSave()
{
// Arrange
var mainForm = new mainForm();
var filePath = @"C:\Users\Hughes Portable Rig\source\repos\GraphicalProgrammingEnvironment\saved_user_input.txt";
var content = "test content";
mainForm.SingleCommandLine.Text = $"save {filePath} \"{content}\"";

// Act
mainForm.ExecuteCommand();

// Assert
Assert.IsTrue(File.Exists(filePath));
Assert.AreEqual(content, File.ReadAllText(filePath));

// Clean up
File.Delete(filePath);
}
}
here is the test itself
hugheselite
hugheseliteOP13mo ago
heres a repo to my entire project
exokem
exokem13mo ago
What string is empty What is the full error or failure and where in your test does it point to
hugheselite
hugheseliteOP13mo ago
one moment I will post it
hugheselite
hugheseliteOP13mo ago
No description
exokem
exokem13mo ago
Which is line 72 in TestSavw Nevermind it's the areequal call
hugheselite
hugheseliteOP13mo ago
No description
exokem
exokem13mo ago
Are you able to verify that an exception does not occur when you try to write to the file? Have you tried debugging the test with breakpoints?
hugheselite
hugheseliteOP13mo ago
when I try to write the file it works as expected I will try my best to show my time spent using the debugger is minimal it's bad I know lol
exokem
exokem13mo ago
It's worth figuring out how to use the debugger It will save you a lot of waiting for help You can step through the code one line at a time to see what is happening
hugheselite
hugheseliteOP13mo ago
!close
Accord
Accord13mo ago
Closed!
Want results from more Discord servers?
Add your server