locked
Cannot create unit test for functions using Isolated Storage RRS feed

  • Question

  • Hi,
    I have created a Class Library project which will be linked to my WPF ClickOnce application. My library is using isolated storage to save application files. 

    I have also created unit tests for my library functions. The problem is when I run my tests I get failed tests with the following reason:
    Failed	OpenStreamTest	StorageTest	Test method StorageTest.IsolatedStorageUtilityTest.OpenStreamTest threw exception:  System.IO.IsolatedStorage.IsolatedStorageException: Unable to determine application identity of the caller..	

    After trawling through the internet I found the following:
    "Note that application scoped isolated storage is only available if your program is running as a ClickOnce application.  If it is not, you'll get an IsolatedStorageException when you call GetUserStoreForApplication which says "Unable to determine the application identity of the caller."
    from http://blogs.msdn.com/shawnfa/archive/2006/01/18/514407.aspx

    which is all well and good, I am planning to use a ClickOnce application, but I would like to create and run the unit tests, obviously.

    So how do I create/run my unit tests? The internet has been somewhat reticent in providing illumination upon this matter. :)

    I created the following function:
    internal IsolatedStorageFileStream OpenStream(string filename)
     {
          / / Obtain the isolated storage for an application.
          return new IsolatedStorageFileStream(filename, FileMode.Open, store);
     }
    

    I have then created a unit test for this function:
    [TestMethod()]
    public void OpenStreamTest()
    {
        string filename = this.ToString() + ".XML";
        MyDataSet myDataSetDS = new MyDataSet ();
    
        //this line throws IsolatedStorageException: Unable 
       //to determine application identity of the caller
        using (IsolatedStorageUtility target = 
                          new IsolatedStorageUtility())
        {
            using (Stream stream =
                           target.OpenStream(filename))
            {
                myDataSetDS.WriteXml(stream);
            }
            //Assert file exists
            //delete isolated storage file
        }        
    }
    
    Thanks for your help,
    Leon
    • Edited by Iobetes Tuesday, March 30, 2010 9:42 AM cosmetic
    Tuesday, March 30, 2010 9:42 AM

Answers

  • Hi Leon,

    I guess the problem is the way how you create the file in isolated storage. It looks like there are ways to get isolated storage files even for non-clickoince applications, something like this should work:

                string fileName = "test.txt";
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForDomain())
                {
                    using (IsolatedStorageFileStream fs = store.CreateFile(fileName))
                    {
                    }
                }

    I would recommend to investigate a little into clickonce isolated storage to see what's best fits you needs. After that you may consider changing your code-under-test API so that it can be easily tested with isolated storage configured outside, such as by test code.

     

    Thanks,
    Michael Koltachev,
    Visual Studio Team Test

     

    • Marked as answer by Iobetes Wednesday, April 7, 2010 8:41 AM
    Monday, April 5, 2010 5:45 PM

All replies

  • Hi Leon,

    I guess the problem is the way how you create the file in isolated storage. It looks like there are ways to get isolated storage files even for non-clickoince applications, something like this should work:

                string fileName = "test.txt";
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForDomain())
                {
                    using (IsolatedStorageFileStream fs = store.CreateFile(fileName))
                    {
                    }
                }

    I would recommend to investigate a little into clickonce isolated storage to see what's best fits you needs. After that you may consider changing your code-under-test API so that it can be easily tested with isolated storage configured outside, such as by test code.

     

    Thanks,
    Michael Koltachev,
    Visual Studio Team Test

     

    • Marked as answer by Iobetes Wednesday, April 7, 2010 8:41 AM
    Monday, April 5, 2010 5:45 PM
  • Hi Michael,

    You are right. It seems the pertinent information was how I created the store. My code used to be:

    using (IsolatedStorageFile storeFiles = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (Stream stream = new IsolatedStorageFileStream("test.xml", FileMode.Open, storeFiles))
                    {
                        dataSet.ReadXml(stream);
                        successful = true;
                    }
                }

    Of course if I'm not using an application then quite rightly VS gets upset. It's all very logical now! :)

     

    Thanks again,

    Leon

    Wednesday, April 7, 2010 8:41 AM
  • We can use Isolated Storage as a virtual file system to store data in a hidden folder on our machine in silverlight application. Silverlight application is allocated the storage of 1 MB per application but we can increase the amount of storage.
    This is the function to get the data in the isolated storage
     
      Private Function LoadData(ByVal fileName As String) As String
            Dim data As String = String.Empty
            Dim isfstream As New IsolatedStorageFileStream(fileName, FileMode.Open, IsolateStorageFileObj)
            Dim sr As New StreamReader(isfstream)
            data = sr.ReadLine()
            Return data
      End Function

    Cheers, Eliza
    Friday, June 11, 2010 11:32 AM
  • It's not really "logical" to me. A unit test is still an application. There's some kind of test runner that launches the tests, whether that is Visual Studio itself or NUnit or whatever.

    I am building infrastructure code that is creating isolated storage files on behalf of the application, and the code itself works just fine. But, I'm not happy with code that is not covered by unit tests.

     

    Thursday, July 1, 2010 6:08 AM
  • Kenneth,

    I am by no means an expert but:

    Yes a unit test is an application but it is not a ClickOnce application. IsolatedStorage application means ClickOnce.

     

    You can still build unit tests for your code, just pass in the IsolatedStorage store to your function. So for your unit test pass a domain wide IsolatedStorage store and in your application pass an application wide store. 

    The alternative, and simpler, solution is to just use a domain wide IsolatedStorage store throughout (do you really need the application wide IsolatedStorage store?): then you can unit test it without changing anything.

     

    Hope this helps,

     

    Leon

    Thursday, July 1, 2010 9:47 AM