9 Replies - 6819 Views - Last Post: 10 February 2009 - 11:58 PM Rate Topic: -----

#1 Bismark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 09

Unit testing problems

Post icon  Posted 13 January 2009 - 08:23 AM

Good day.
I'm just beginning to study C# and, particularly, unit testing. So, here is part of the program, that logs various errors into a file.
/// <summary>
		/// Logs error into File and/or EventLog
		/// </summary>
		/// <param name="moduleName">Module name where error occured</param>
		/// <param name="error">Error description</param>
		public void LogError(string moduleName, string error)
		{
			LogRecord(RecordTypes.Error, moduleName, error);
			LogEvent(EventLogEntryType.Error, moduleName, "ERROR: " + error, 0);
		}

I'm using Visual Studio 2008. I create a unit test, and it looks like
[TestMethod()]
public void LogErrorTest()
		{
			Logger_Accessor target = new Logger_Accessor(); // TODO: Initialize to an appropriate value
			string moduleName = string.Empty; // TODO: Initialize to an appropriate value
			string error = string.Empty; // TODO: Initialize to an appropriate value
			int eventID = 0; // TODO: Initialize to an appropriate value
			target.LogError("moduleName", error, eventID);
			Assert.Inconclusive("A method that does not return a value cannot be verified.");
		}

When I write something like "FooModule" instead of "Empty" in "string.Empty", it gives me an error CS0117 'string' does not contain a definition for 'FooModule'. What am I doing wrong?
Thank you for the answer.

Is This A Good Question/Topic? 0
  • +

Replies To: Unit testing problems

#2 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Re: Unit testing problems

Posted 13 January 2009 - 02:57 PM

Try
string moduleName = "FooModule"; // TODO: Initialize to an appropriate value



Using string.FooModule is like saying you want to use the 'FooModule' part of 'string'. Of course there is no such thing so it gives you the error you mentioned above. Simply putting the text you want 'moduleName' to equal in "" marks implies that it is a string.
Was This Post Helpful? 0
  • +
  • -

#3 Bismark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 09

Re: Unit testing problems

Posted 14 January 2009 - 02:00 AM

Thanks.
Now it gives me another error
Unable to get type LogError_System.String__System.String_system.Int32_.LoggerTest, LogError(System.String, System.String,system.Int32). Error: System.IO.FileLoadException: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

Any ideas on this problem?
Was This Post Helpful? 0
  • +
  • -

#4 Bismark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 09

Re: Unit testing problems

Posted 14 January 2009 - 04:21 AM

Nevermind, it was a VS 08 bug. Re-creating a unit test terminated it.
Was This Post Helpful? 0
  • +
  • -

#5 Bismark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 09

Re: Unit testing problems

Posted 14 January 2009 - 08:36 AM

One more question:
I'm willing to do a unit test, that checks the TypeInitializationException. So, I do the try-catch cycle like this:
public void LogErrorTest()
		{
			try
			{
				Logger_Accessor target = new Logger_Accessor(); 
				string error = string.Empty; 
				target.LogError(error);
			}
			catch (System.TypeInitializationException e)
			{
			}

This test passes, even when the program throws no exception. But I want it to fail in that case. What should I do?
Thank you for the answer.
Was This Post Helpful? 0
  • +
  • -

#6 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Re: Unit testing problems

Posted 14 January 2009 - 01:29 PM

I haven't done unit testing in VS before (just with Java in Eclipse) but I'm guessing that

target.LogError(error)

is supposed to throw an exception? If that's true then you could put something along the lines of Assert.Fail (I'm not sure of the exact syntax) under target.LogError(error) to try and manually fail the test if the program gets that far (if there is an exception in target.LogError then it will go straight to the catch statement and not execute the Assert.Fail line of code).

Maybe someone else has a better idea of how to do this as well.
Was This Post Helpful? 1
  • +
  • -

#7 Bismark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 09

Re: Unit testing problems

Posted 21 January 2009 - 06:18 AM

Thanks, your answer was very helpful!

I've added Assert.Fail to the end of the code, and a "return" into catch sequence, and it works just perfectly.

This post has been edited by Bismark: 21 January 2009 - 07:27 AM

Was This Post Helpful? 0
  • +
  • -

#8 Bismark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 09

Re: Unit testing problems

Posted 22 January 2009 - 01:01 AM

Hi again!

The next problem I have is connected with xml. The program throws an exception:

Test method ExcLogErrorSSI.LoggerTest.LogErrorTest threw exception:  System.TypeInitializationException: The type initializer for 'Centro.Utils.Config' threw an exception. --->  System.InvalidOperationException: There is an error in XML document (1, 1). --->  System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1..


I've looked up in the Internet and found some closely connected issues. One of them is about writing Load() instead of LoadXML(), but I don't have these methods in the program. Another is about StreamWriter having a problem with utf-8. I've tried to convert my xml file to utf-16 and some other encoding, but either I didn't managed to do it or that's not the problem. Here is the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<CentroConfig>
	<Log WriteInFile="True" WriteInEventLog="True" LogPath="C:\CentroLogs\" />
	<ConfigParameters>
		<Config Key="CrmServiceUrlTemplate" Value="http://{SERVER}{PORT}/MSCRMServices/2007/CrmService.asmx" />
		<Config Key="CrmDeploymentServiceUrlTemplate" Value="http://{SERVER}{PORT}/MSCRMServices/2007/CrmDeploymentService.asmx" />
		<Config Key="CrmDiscoveryServiceUrlTemplate" Value="http://{SERVER}{PORT}/MSCRMServices/2007/{SERVICETYPE}/CrmDiscoveryService.asmx" />
	</ConfigParameters>
</CentroConfig>

Anyone knows what's the problem?

Update: SOLVED, the program addressed the wrong xml.

This post has been edited by Bismark: 22 January 2009 - 04:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 Bismark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 09

Re: Unit testing problems

Posted 22 January 2009 - 09:43 AM

This issue is connected with reading from a file. I need to check the log automatically in the unit test. The log is made by this piece of code:
StringBuilder formatString = new StringBuilder();
			if (string.IsNullOrEmpty(recordType))
			{
				if (string.IsNullOrEmpty(moduleName))
				{
					formatString.AppendFormat("{0}\t{1}", GetTimeStamp(), info);
				}
				else
				{
					formatString.AppendFormat("{0}\t'{1}':\t{2}", GetTimeStamp(), moduleName, info);
				}
			}
			else
			{
				if (string.IsNullOrEmpty(moduleName))
				{
					formatString.AppendFormat("{0}\t{1}:\t{2}", GetTimeStamp(), recordType, info);
				}
				else
				{
					formatString.AppendFormat("{0}\t{1} IN '{2}':\t{3}", GetTimeStamp(), recordType, moduleName, info);
				}
			}

			writer.WriteLine(formatString.ToString());
			writer.Flush();

I want to check, if the line I get matches the line "{0}\t{1} IN '{2}':\t{3}", GetTimeStamp(), recordType, moduleName, info. Somebody knows the answer?
Was This Post Helpful? 0
  • +
  • -

#10 Bismark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 09

Re: Unit testing problems

Posted 10 February 2009 - 11:58 PM

Hi everyone!
Today I've encountered a problem of several consecutive tests. I'm testing the same logging problem, here is one of my unit tests:
[TestMethod()]
		public void LogErrorTest()
		{
			Logger_Accessor target = new Logger_Accessor(); 
			string moduleName = "Module5"; 
			string error = "Error 404"; 
			int eventID = 404; 
			target.LogError(moduleName, error, eventID);
			string time = Logger_Accessor.GetTimeStamp();
			string exp = string.Format("{0}\tERROR IN '{1}':\t{2}\r\n", time, moduleName, error);
			using (FileStream file = new FileStream(Config.LogFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
			{
				StreamReader sr = new StreamReader(file);
				string s = sr.ReadToEnd();
				if (exp.Equals(s))
				{
					return;
				}
				else
				{
					Assert.Fail("Logged string differs from expected.");
				}
			}
		}

The problem begins when I start several tests: one of them passes, the rest fail saying "The process cannot access the file because it's being used by another process". How can I clear memory or somehow close the file before the next test starts?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1