我正在尝试使用Sqlite为ServiceStack服务编写单元测试。因为Sqlite不支持存储过程,所以我创建了“ServiceCommand”类型的公共属性,该属性将命令文本和命令类型作为输入。默认情况下,它被配置为运行存储过程,并且在编写单元测试用例时,我在调用SelectCommand方法之前,将Any()属性重新分配给sql查询,如下所示。所有测试用例都能正常工作。
var request = new CustomerRequest() { name = "alfki" };
var service = new CustomerService(InMemoryTes
我试图为自己定义一些依赖注入指南。在为要通过构造函数或setter注入注入的类定义依赖关系时,什么应该是正确的粒度?类可以是服务、存储库等。假设有一个存储库类,如下所示:
public class ProductRepository
{
//Option-A
public ProductRepository(DataSource dataSource)
{
}
//Option-B
public ProductRepository(SqlSession sqlSession)
{
}
//Option-C
如果我试图构造单个实例的新实例,Guice会抛出异常吗?
例如:
public class MyModule extends AbstractModule {
@Override
protected void configure() {
bind(MySingleton.class).in(Singleton.class);
}
}
@Singleton
public class MySingleton {
MySingleton() { /* ... */ }
}
public class RightWay {
public voi
我读过很多关于单元测试时Application.Current.Dispacher为null的文章。这正是我遇到的问题。我找到了,这正是我要找的。然而,当我去实现这一点时,我似乎无法编译。犯了以下错误:
无法在这里访问私有构造函数“Dispatcher”。
在上面的文章中,他们说有很多方法来实现我下面的代码。因为他还没检查过多线程安全检查。我如何进行这些检查并将其编译?
public static Dispatcher RootDispatcher
{
get
{
if (_rootDispatcher == nu
我在这个问题上找到了,并尝试了以下几点:
public class FailerAttr : Attribute {
public FailerAttr(string s) {
throw new Exception("I should definitely fail!");
}
}
在单元测试项目中,我有以下内容:
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class Test {
[TestMethod]
public voi
我所处的情况是,我正在编写单元测试用例来测试代码,并且我遇到了一种无法为PrivateObject和PrivateType创建对象的情况。我找了很多,但没有找到有用的东西。最后提出问题寻求帮助。我使用的是MSFakes测试。
下面是我的示例代码:
public class A
{
//this class is not having default constructor and I can't modify the code as well.
public A(int id){
//some code
}
private void Metho
CustomerService是一个webservice,它正在被成功调用,但我无法在任何方法中访问SelectCommand。我想我在这里漏掉了什么,有人能告诉我。
public class CustomerService : Service
{
private readonly IDbConnection _dbConnection; // injected successfully
public ServiceCommand SelectCommand {get;set;}
public CustomerService(IDBConnection dbConnection)
我创建了一个泛型类,如下所示,当我在Visual中对程序进行编码和测试时,这个类工作得很好。但是,在自动生成中,运行的单元测试无法处理程序集,尽管以前没有看到编译或运行时错误:
错误18 :在处理程序集‘.dll’时发生错误:提供的泛型参数的数量不等于泛型类型定义的正确性。参数名称:实例化...Test
单元测试不使用泛型类。
我注释掉了类中的所有代码、构造函数和字段,然后用Visual自动实现接口,生成带有“抛出新NotImplementedException()”的存根。
public class RequiredPropertiesProfile<TPropertyGroup, T
我无法对包含HttpContext.Current.Request.LogonUserIdentity.Name的函数之一进行单元测试
我得到的错误是因为LogonUserIdentity为空。
我在单元测试中添加了以下内容,以便为HttpContext.Current设置一个值,因为它也是空的:
HttpContext.Current = new HttpContext(
new HttpRequest("", "http://localhost:8609", ""),
new HttpRespo
我假设使用模型-视图-展示者( Model-View-Presenter,MVP)模式编写单元测试会容易得多,但我甚至不确定如何开始编写单元测试。我已经添加了依赖项,开始使用Roboelectric和Mockito编写单元测试,以模拟我的presenter中使用的所有依赖项。
作为参考,下面是我的演示者登录的示例:
public class LoginPresenterImpl implements ILoginPresenter {
@Inject
Bus bus;
@Inject
ISharedPreferencesRepository mSharedPreferencesReposi
我有一个Carpenter类,它使用Lathe和Wood对象完成它的工作。
class Carpenter
{
function Work()
{
$tool = new Lathe();
$material = new Wood();
$tool->Apply($material);
}
}
Lathe依赖于一个名为Material的接口,因此我可以通过在单元测试中给出一个假的Material来轻松地对Lathe进行单元测试。Wood不依赖任何东西,因此也可以很容易地进行测试。
interface Material
我有一个带有注入属性的摘要。在单元测试中,@InjectMocks为抽象类中注入的属性提供null。
请参阅下面的代码。
我想知道为什么在这种特定的情况下,@InjectMocks不知道从抽象类注入属性。有人能帮我理解吗?非常感谢!
如果子类和父类都使用属性注入,那么我的单元测试就可以正常工作。
public abstract class AbstractClass
{
@Inject
private D d;
...
}
@RequestScoped
public class ConcreteClass extends AbstractClass
{
private
我能够模拟会话,但是我无法成功地模拟sessionconfiguration类。如果您在文章底部看到类,请调用我类的connect方法。在调试connect方法中的Settings对象时,始终为空。你能告诉我有什么问题吗?
我有一门课叫XYZ。它在它中声明了FixSessionConfiguration依赖项
Class XYZ
{
[Dependency]
public FixSessionConfiguration Settings
{
get;
set;
}
public
我有一个单元测试,它通过UI进入系统中的用户,但在此之前,它删除了该用户的现有条目
我有3个查询集合,每个集合只有1条我想要删除的记录,但在我的单元测试中,它无法执行delete查询并返回超时错误
我不知道如何优化查询,如果有人可以在这方面提供帮助
delete from CustomerRoles where RegisteredCustomerId = (select Id from RegisteredCustomers where Email = 'boltestsignupseller@yahoo.com')
delete from SellerInfos whe
当尝试在TFS 2015 (vNext)中作为构建步骤运行单元测试时,我会收到以下警告:
Warning: Unable to create DiaSession for <assembly>
No source location data will be available for this assembly.
然后运行测试程序集,但是内部的单元测试会失败,因为它们无法找到引用,我假设是因为警告的“无源位置数据将不可用”部分。
我用于单元测试的NUnit NuGet包是:"NUnitTestAdapter.WithFramework“v2.0.0。这个项目是用C#制作的。