Reading an asset in a Xamarin NUnit test class

余生颓废 提交于 2019-12-12 05:30:09

问题


I want to read a mock RSS feed file to test against in a Xamarin NUnit test for an Android app. The test class has no context so I can't put the file in the Assets folder and do this:

System.IO.Stream strIn = instanceOfAssetManager.Open(feedFile);

It also has no ancestor, so I can't do this:

System.IO.Stream strIn = this.Class.ClassLoader.ResourceAsStream(feedFile);

Any suggestions how I can get to the jolly thing?

This case solved a similar problem for a different platform/setup: Storing test files in the test project


回答1:


The test class has no context so I can't put the file in the Assets folder and do this:

Is there a reason you need to use Android Assets? Using .NET Embedded Resources by setting the file's Build action to EmbeddedResource permits greater portability -- it's present on all .NET platforms -- and neatly sidesteps the Android Context issue.

If you need to use Android Assets, then you can use the Android.App.Application.Context property, which is equivalent to the Context.ApplicationContext property, except static and available everywhere.




回答2:


Check properties of the file. I had to set "Build Action" to AndroidAsset and than you can use instanceOfAssetManager.Open(feedFile);



来源:https://stackoverflow.com/questions/19227767/reading-an-asset-in-a-xamarin-nunit-test-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!