2

There is [SetUp] part where some file is loaded for later tests.

It should be loaded with:

private string GiveFilePath()
{
   string folder;
   string assemblyPath;

   assemblyPath = Assembly.GetExecutingAssembly().Location;
   folder = new FileInfo(assemblyPath ).DirectoryName;
   return folder + @"\" + FileNameAsConst;
}

But I get

System.IO.FileNotFoundException in C:\Users\username\AppData\Local\Temp\...

cause its trying to load this file from AppData and not from the projects folder.

There is the similiar question asked already. And the answer, that the author of the question have found himself is to set ShadowCopyFiles property to true.

But there is no any ShadowCopyFiles in code or in config files.

Do you have any ideas?

Update:

[SetUp]
public void SetUp()
{       
    this.facade = new MyFacade();
}

[Test]
public void InstanceIsNotNull()
{
   Assert.IsNotNull(facade);
}

...

public MyFacade()
{
   FileStream fileStrem = new FileStream(GiveFilePath(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
   ...
}
Community
  • 1
  • 1
MikroDel
  • 6,705
  • 7
  • 39
  • 74
  • Does the file actually exist? I know it sounds trivial but it happens quite often that there is a typo of some sort. – Matthijs Aug 06 '14 at 06:23
  • well I guess your testrunner is storing it's testassemblies somewhere in your *Home* directory (the path you gave is for *windows* what /home/[username] is for linux ;) ) - and this should not concern you - just make sure that you include copies of the files you need – Random Dev Aug 06 '14 at 06:33
  • Well, you need to be aware of the context the test is running on. Appreciate if you provide more details about what exactly you are doing in these tests because the answers might differ depending on what's actually being done in these tests – Leo Aug 06 '14 at 06:33
  • btw: in similar scenarious I just use relative paths (".\", "..\", etc.) and this works fine with the integrated testrunner, resharpers testrunner and NCrunch ... – Random Dev Aug 06 '14 at 06:34
  • @Matthijs this file exist under ProjectName fodler – MikroDel Aug 06 '14 at 06:37
  • @CarstenKönig - "just make sure that you include copies of the files you need". This file is stored in ProjectName folder. Is that what you mean? Or should I do something else? – MikroDel Aug 06 '14 at 06:39
  • The file should exist at the location where your code is pointing too. From my understanding that is not the case right here... Either change the code to the corresponding location or move the file. – Matthijs Aug 06 '14 at 06:40
  • @Leo I want to test that some class instance is created and not null. But the exception occur on SetUp part - before my test is executed – MikroDel Aug 06 '14 at 06:42
  • @Matthijs - if I use the code shown to get current assembly and path to it - why the test runner try to search under AppData? – MikroDel Aug 06 '14 at 06:43
  • @MikroDel: debug through your program and see how the path is assigned. Also include your unittest-code. Last, try running your unittest seperatly from other unittests; other unittests may interfere with some data used by the unittest. – Matthijs Aug 06 '14 at 06:46
  • @Matthijs - surely I debugged it before :) In debug mode I have seen that is started to search under AppData. – MikroDel Aug 06 '14 at 06:47
  • @Matthijs my unit test started separately. The code shouldnt be shown, cause its not even starte. The exception occurs on SetUp part. – MikroDel Aug 06 '14 at 06:48
  • @MikroDel: please include the unittest for this particular piece of code so we can try and debug it too. – Matthijs Aug 06 '14 at 06:48
  • I agree with @MikroDel, the code in the unit tests are irrelevant. More importantly what kind of files are trying to load and how is that file deployed? – Leo Aug 06 '14 at 06:50
  • @Leo: I checked his particular piece of code, the one that is throwing the exception. It doesn't go to that folder for me; so something that is calling this method seems to be altering the location from where it started. And since it is being called through the unittest, it may alter the location. – Matthijs Aug 06 '14 at 06:51
  • @Leo - the file is xml one – MikroDel Aug 06 '14 at 06:55
  • @Matthijs actually, you are right...I was thinking of `SetupFixture` that is executed before any tests – Leo Aug 06 '14 at 06:56
  • @MikroDel...did you even try the answer provided? – Leo Aug 06 '14 at 06:59
  • @Leo I have answered the comments step by step and after it I also try the answer :) – MikroDel Aug 06 '14 at 07:01

2 Answers2

1

You should be aware of the context the test is running on and the context of the code you have provided whether the executing assembly is the actually test or an external assembly you are loading into a separate (or same) AppDomain, etc.

Most of the time, if you are not doing anything special you wouldn't need a reflection call to Assembly.GetExecutingAssembly if the file you're trying to load is located in the same place as the executing assembly. You can easily use the classes in the System.IO namespace to manage this file which is less expensive than any reflection operation. You don't even need to specify the whole absolute path, a relative path will suffice. If the file is included in your project, make sure you set the "Copy to Output Directory" to "Copy always" and also make sure the "Build Action" is set to "Content", then, at runtime, first make sure the file exists...

if(File.Exists("filename.ext"))

notice that the path is relative, and then do whatever you want with the file...

File.ReadAllText("filename.ext");
File.Delete("filename.ext");
Leo
  • 14,625
  • 2
  • 37
  • 55
1

I strongly suspect that something is happening with the assemblylocation prior to entering the unittest, which is why I recommend a different setup of your unittest.

Try following the AAA-pattern:

Arrange: setup everything needed for the running the tested code. This includes any initialization of dependencies, mocks and data needed for the test to run.
Act: Invoke the code under test.
Assert: Specify the pass criteria for the test, which fails it if not met.

Your unittest certainly unclarifies the following:

  • What was actually going on in the test
  • What data was being used
  • What was being tested

Taken from this link.

From the code you provided, there is no clue whether other tests may have interfered with the data being used. It could very well alter the setup of facade by different cultures or other alterations.

I recommend you alter your unittest to the AAA-pattern and isolate the issue. So change setup to be inside the unittest to eliminate any changes being made from other locations.

Also run this unittest alone, without running anything else. I've seen a unittest fail because the culture had been changed in one unittest prior to running the unittest I was concerned about (and it failed because of this culture-change).

If the problem still persists after isolating the unittest; provide reproducable code that gives you this issue.

Matthijs
  • 3,162
  • 4
  • 25
  • 46
  • "From the code you provided, there is no clue whether other tests may have interfered with the data being used. It could very well alter the setup of facade by different cultures or other alterations" I have answered you already in the comments to the question - the test start isolated from other test. – MikroDel Aug 06 '14 at 07:19
  • @MikroDel: can you provide a reproducable case then? – Matthijs Aug 06 '14 at 07:21
  • I have provided my code already. My question was updated after your comments – MikroDel Aug 06 '14 at 07:23
  • @MikroDel: You have provided a constructor which is not being called, as you are calling a constructor which takes in a parameter: "logger". How am I supposed to reproduce this? – Matthijs Aug 06 '14 at 07:27
  • Sorry logger is not needed to reproduce it – MikroDel Aug 06 '14 at 07:31
  • I have solved it with the Leos answer. Thank you for trying to help me +1. – MikroDel Aug 06 '14 at 07:32