C
C#14mo ago
Spekulant

❔ xUnit tests

using System;
using System.IO;

namespace TestWordCount;

public class WordCounterTests
{
[Fact]
public void Test1() {
var story = """
Jack and Jill went up the hill.
""";
var reader = new StringReader(story);

var counter = new WordCounter(reader);

counter.Execute();
var stringWriter = new StringWriter();
Console.SetOut(stringWriter);
counter.WriteResults();
string allConsoleOutput = stringWriter.ToString();
Console.WriteLine("Actual Output: " + allConsoleOutput);


Assert.Equal("14\n2", allConsoleOutput);

}



}
using System;
using System.IO;

namespace TestWordCount;

public class WordCounterTests
{
[Fact]
public void Test1() {
var story = """
Jack and Jill went up the hill.
""";
var reader = new StringReader(story);

var counter = new WordCounter(reader);

counter.Execute();
var stringWriter = new StringWriter();
Console.SetOut(stringWriter);
counter.WriteResults();
string allConsoleOutput = stringWriter.ToString();
Console.WriteLine("Actual Output: " + allConsoleOutput);


Assert.Equal("14\n2", allConsoleOutput);

}



}
i have this code that has function execute it reads text and appends the words in a paragraph into a list. then a function write results it writes the results onto a commandline for example
12
4
12
4
the code alone works perfectly fine when im debugging the xunit tests it execute counts the words correctly but when the writeResults function starts the values in the list are empty
10 Replies
Spekulant
SpekulantOP14mo ago
I probably found the error the var story does not have EOF thing at the end that is causing an issue
Spekulant
SpekulantOP14mo ago
it doesnt work when i have it declared like this
No description
Spekulant
SpekulantOP14mo ago
or this but only when like this
No description
Spekulant
SpekulantOP14mo ago
No description
Spekulant
SpekulantOP14mo ago
but i would like to use the first method beucase i want use test cases when there are
ADJSKFNADFOBFABSDbasdnfasbfd
aisbdgfa9dnfan
ba9sdbfas9dnf
sa9dnfa

dab9sdgasbg
absd9fa
bab9
ADJSKFNADFOBFABSDbasdnfasbfd
aisbdgfa9dnfan
ba9sdbfas9dnf
sa9dnfa

dab9sdgasbg
absd9fa
bab9
something like this Is there any way that i can fix this
Pobiega
Pobiega14mo ago
First, its unusual to test output rather than data. If you structured your code so your WordCounter exposed its count as an int property or return value, this would be trivial to test. Second, you redirect the console output in your test, then without resetting it you do Console.WriteLine again. a unit test should never write anything to stdout anyways, so thats just.. wrong you do the thing, you assert its value. thats it.
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Spekulant
SpekulantOP14mo ago
what if my normal code writes thing in the console if the data is wrong
Pobiega
Pobiega14mo ago
It shouldn't really, thats a presentation specific task I get a strong feeling you didn't read all my 4 messages above before asking thou
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server