我正在读Jon Skeet的书"C#in Depth,2nd Edition".他说我们可以使用动态参数调用扩展方法,使用两种解决方法,就像
dynamic size = 5;
var numbers = Enumerable.Range(10, 10);
var error = numbers.Take(size);
var workaround1 = numbers.Take((int) size);
var workaround2 = Enumerable.Take(numbers, size);
Run Code Online (Sandbox Code Playgroud)
然后他说"如果你想用动态值作为隐含this值调用扩展方法,两种方法都会有效".我不知道如何实现它.
非常感谢.
我正在使用Maven和多模块.有3个项目.
foo(the parent project)
foo-core
foo-bar
Run Code Online (Sandbox Code Playgroud)
我在foopom中配置所有依赖项和插件:
<modules>
<module>../foo-core</module>
<module>../foo-bar</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
...
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.14.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
Run Code Online (Sandbox Code Playgroud)
有几个基类和util类用于单元测试foo-core,所以我添加了maven-jar-pluginin foo-core项目以使其可用于foo-bar:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
当我执行test …
我想知道是否有任何方法在C#中实现通用的空对象模式.泛型null对象是所有引用类型的子类,就像Nothing在Scala中一样.这好像是
public class Nothing<T> : T where T : class
Run Code Online (Sandbox Code Playgroud)
但它无法编译,我不知道如何实现T提供默认行为或抛出异常的方法.以下是一些想法:
Nothing<T>?它可能看起来像Moq.另一个问题是:在产品代码中使用模拟框架/库是否可以?我知道也许我应该为特定类型实现特定的null对象.我只是想知道是否有任何解决方案.
有什么建议吗?谢谢.
在一般情况下,接口或抽象类通常是适当的决定,对吗?
但在某些情况下,看起来具体类更好.例如,
public string Replace(string old, string new)
Run Code Online (Sandbox Code Playgroud)
返回具体类的Replace方法String.(这只是一个例子,虽然String没有实现任何接口.)
我的问题是
什么时候应该返回一个接口,什么时候应该返回一个具体的类?
它是program to an interface, not an implementation返回界面的一部分吗?
当我测试此静态方法时
public class SomeClass {
public static long someMethod(Map map, String string, Long l, Log log) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
与
import org.apache.commons.logging.Log;
@RunWith(PowerMockRunner.class)
//@PrepareForTest(SomeClass.class)
public class Tests {
@Test
public void test() {
...
PowerMockito.mockStatic(SomeClass.class);
Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), isA(Log.class))).thenReturn(1L);
...
}
}
Run Code Online (Sandbox Code Playgroud)
我知道了InvalidUseOfMatchersException。我的问题是:
isA(Log.class)返回null。@PrepareForTest注释添加到测试类并运行测试时,junit不会响应。为什么?编辑
我试图不使用参数匹配器,并得到
org.mockito.exceptions.misusing.MissingMethodInvocationException:when()需要一个参数,该参数必须是“模拟的方法调用”。例如:when(mock.getArticles())。thenReturn(articles);
同样,由于以下原因,可能会出现此错误:
您存根以下方法之一:final / private / equals()/ hashCode()方法。这些方法不能存根/验证。
在when()内部,您不会在模拟对象上调用方法,而是在其他对象上调用方法。
在 ...
因此,似乎是由于someMethod本身。该方法中有同步块。我想知道Powermockito是否可以模拟这种方法。
谷歌搜索时,我在堆栈溢出中发现了这个问题,但它已被删除。因此,我再次列出了这个问题。
由于我在BCL中找不到LcidAttribute或RetvalAttribute,因此我猜C#没有提供对语言环境标识符参数和返回值参数的支持。
是吗
谢谢大家
c# ×4
dynamic ×1
interface ×1
java ×1
maven ×1
mockito ×1
powermock ×1
reflection ×1
unit-testing ×1