我有这样的测试:
it "should not indicate backwards jumps if the checker position is not a king" do
board = Board.new
game_board = board.create_test_board
board.add_checker(game_board, :red, 3, 3)
x_coord = 3
y_coord = 3
jump_locations = {}
jump_locations["upper_left"] = true
jump_locations["upper_right"] = false
jump_locations["lower_left"] = false
jump_locations["lower_right"] = true
adjusted_jump_locations = @bs.adjust_jump_locations_if_not_king(game_board, x_coord, y_coord, jump_locations)
adjusted_jump_locations["upper_left"].should == true
adjusted_jump_locations["upper_right"].should == false
adjusted_jump_locations["lower_left"].should == false
adjusted_jump_locations["lower_right"].should == false
end
Run Code Online (Sandbox Code Playgroud)
我知道,这是冗长的.是否有更简洁的方式来陈述我的期望.我看过文档,但我看不出在哪里压缩我的期望.谢谢.
class Foo
def bar(a, b)
...
Foo.should_receive( :bar )
Run Code Online (Sandbox Code Playgroud)
期望使用任何参数调用bar.
Foo.should_receive( :bar ).with( :baz, :qux )
Run Code Online (Sandbox Code Playgroud)
期待:baz和:qux作为params传入.
如何期待第一个参数平等:baz,而不关心其他参数?
在使用JMockit时,我想在构造函数调用上抛出异常,如下所示:
new Expectations(){
{
new FirefoxDriver();//Want to throw IllegalStateException here but how?
}
};
Run Code Online (Sandbox Code Playgroud) rhino-mocks存根和期望有什么区别:在我看来他们的行为完全相同?
mockContext.Stub(x => x.Find<Blog>())
.Return(new List<Blog>()
{
new Blog() { Id = 1, Title = "Test" }
}.AsQueryable());
mockContext.Expect(x => x.Find<Blog>())
.Return(new List<Blog>()
{
new Blog(){Id = 1,Title = "Title"},
new Blog(){Id=2,Title = "no"}
}.AsQueryable());
Run Code Online (Sandbox Code Playgroud) 规格:
before do
Logger.should_receive( :write ).with 'Log message 1'
end
it 'works' do
get '/'
end
Run Code Online (Sandbox Code Playgroud)
Sinatra App:
get '/'
Logger.write( 'Log message 1' )
Logger.write( 'Log message 2' )
end
Run Code Online (Sandbox Code Playgroud)
由于"Log message 2",此规范失败.如何告诉RSpec忽略任何其他消息,并只测试预期的消息?
在谷歌文档模拟说:
重要提示: Google Mock 要求在调用模拟函数之前设置期望值,否则行为是undefined。特别是,您不能将 EXPECT_CALL() 和对模拟函数的调用交织在一起。
有谁知道这个限制背后的任何细节?我有许多单元测试肯定违反了这条规则,但是似乎运行正常。
有没有办法告诉phpunit模拟对象,如果没有正式定义的期望,永远不要指望方法调用?
使用PHPUnit,我想知道如何从同一个存根/模拟中获得多个期望.
例如,我想测试mock将display()调用该方法并返回NULL.我还想测试一下这个方法process()是否会被调用.
事实上,我的测试被称为testProcessIsCalledIfDisplayReturnNull().
所以我需要在同一个模拟对象上设置2个期望值,并且手册并没有真正帮助:(
我有一系列使用 XCTest 框架编写的单元测试。这些最初是在 iOS7 上创建的,然后在 iOS8 设备上的 xCode6 中执行。测试按顺序执行,但随后我得到以下代码块的 EXC_BAD_ACCESS(代码= 1,地址 0xc)。当使用 xcode 中的“Test”命令执行测试时会发生这种情况。
如果我从测试左侧面板单独执行此测试,它会正常通过或失败。
这就是我认为我正在做的事情:
如何确保我可以毫无例外地执行所有单元测试?
//单元测试
-(void)testNetworkDataSourceUpdate
{
self.expectation = [self expectationWithDescription:@"Getting network datasource"];
DataSource* dataSource = [DataSourceProvider datasourceWithRefreshDelegate:self];
XCTAssertNotNil(dataSource, @"Should have datasource immediately available");
//Bad access here
[self waitForExpectationsWithTimeout:10.0 handler:^(NSError *error) {
}];
}
Run Code Online (Sandbox Code Playgroud)
//打回来
-(void)refreshDatasource:(NSMutableArray*)datasource
{
[self.expectation fulfill];
}
Run Code Online (Sandbox Code Playgroud) 我似乎无法在网上找到解决方案。
这是一个代码示例,因此您会遇到问题:
// Spy on the wanted function
spyOn(object, 'myFunction');
// Call it 3 times with different parameters
object.myFunction('');
object.myFunction('', 0);
object.myFunction('', 0, true);
// Now all of these expects work
expect(object.myFunction).toHaveBeenCalledTimes(3);
expect(object.myFunction).toHaveBeenCalledWith('', 0);
expect(object.myFunction).toHaveBeenCalledWith('');
expect(object.myFunction).toHaveBeenCalledWith('', 0, true);
Run Code Online (Sandbox Code Playgroud)
我想测试是否每次调用都正确。有没有办法说这样的话?
expect(object.myFunction).nthCall(2).toHaveBeenCalledWith('', 0, true);
Run Code Online (Sandbox Code Playgroud)
???
expectations ×10
mocking ×4
rspec ×3
ruby ×3
phpunit ×2
unit-testing ×2
c++ ×1
googlemock ×1
googletest ×1
ios8 ×1
jasmine ×1
javascript ×1
jmockit ×1
objective-c ×1
rhino-mocks ×1
sinatra ×1
stub ×1
stubbing ×1
xctest ×1