在Xamarin NUnit测试课程中读取资产 [英] Reading an asset in a Xamarin NUnit test class

查看:100
本文介绍了在Xamarin NUnit测试课程中读取资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取一个模拟RSS feed文件,以在针对Android应用程序的Xamarin NUnit测试中进行测试.测试类没有上下文,因此我无法将文件放在Assets文件夹中并执行以下操作:

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

推荐答案

测试类没有上下文,因此我无法将文件放在Assets文件夹中并执行以下操作:

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

您是否有理由需要使用Android资产?通过设置文件的生成操作EmbeddedResource允许更大的可移植性-它存在于所有.NET平台上-巧妙地避开了Android上下文问题.

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.

如果您需要使用Android Assets,则可以使用 Android.App.Application.Context 属性,它等效于

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.

这篇关于在Xamarin NUnit测试课程中读取资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆