我主要使用GoogleMock的有序期望,因此所有内容EXPECT_CALL都写在testing::InSequence对象的范围内.
现在我想放松排序,所以我将期望分成2个序列.你会说测试应该通过,但是没有 - 它失败了,抱怨未满足的前提条件.我该如何解释这个?
编辑:我的代码的缩减版本:
//InSequence s; // uncomment this and it works
for (int i = 1; i <= 2; ++i)
{
{
//InSequence s; // uncomment this and it doesn't work
EXPECT_CALL(mock1, produceMessage(_))
.WillOnce(DoAll(SetArgReferee<0>(val1), Return(false)))
.WillOnce(DoAll(SetArgReferee<0>(val2), Return(false)))
.WillOnce(DoAll(SetArgReferee<0>(val2), Return(false)));
EXPECT_CALL(mock2, handleEvent(A<MyType>()));
EXPECT_CALL(mock2, handleMessage(NotNull()));
}
}
Run Code Online (Sandbox Code Playgroud)
因此,如果InSequence嵌套在for循环中,我应该有一个部分顺序,这是一个宽松的要求,与InSequence在外面的情况相比.
我得到的错误:
Mock function called more times than expected - returning default value.
Function call: handleMessage(0xd7e708)
Returns: false
Expected: to be called once
Actual: called twice - …Run Code Online (Sandbox Code Playgroud) 我想做这样的事情:
TEST(MyTestFixture, printAfterExpectationFailure)
{
const string request("bring me tea");
const string&& response = sendRequestAndGetResponse(request);
checkResponseWithExpectarions1(response);
checkResponseWithExpectarions2(response);
checkResponseWithExpectarions3(response);
checkResponseWithExpectarions4(response);
if (anyOfExpectsFailed())
cout << "Request: " << request << "\nresponse: " << response << endl;
}
TEST(MyTestFixture, printAfterAssertionFailure)
{
const string request("bring me tea");
const string&& response = sendRequestAndGetResponse(request);
doWhenFailure([&request, &response]()
{
cout << "Request: " << request << "\nresponse: " << response << endl;
});
checkResponseWithAssertion1(response);
checkResponseWithAssertion2(response);
checkResponseWithAssertion3(response);
checkResponseWithAssertion4(response);
}
Run Code Online (Sandbox Code Playgroud)
我仅在期望/断言失败时才打印一些其他信息。
我知道我可以做这样的事情:
#define MY_ASSERT_EQ(lhr, rhs, message) if(lhs != rhs) ASSERT_EQ(lhs, rhs) …Run Code Online (Sandbox Code Playgroud) 我有以下场景:
class InterfaceA;
class InterfaceB;
class InterfaceC;
class InterfaceA
{
virtual void foo(InterfaceC&) = 0;
};
class InterfaceB
{
virtual void bar() = 0;
};
class InterfaceC
{
virtual void bla() = 0;
};
// MOCKs
class MockA : public InterfaceA
{
public:
MOCK_METHOD0(foo, void(InterfaceC&));
};
class MockB : public InterfaceB
{
public:
MOCK_METHOD0(bar, void());
};
class ImplC : public InterfaceC
{
public:
ImplC(InterfaceA& a, Interface& b) : m_a(a), m_b(b) {}
void doSomething() {
m_a.foo(*this);
}
virtual void bla()
{ … 据我所知,should_receive仅适用于模拟对象.我想要检查,如果某个类(非对象)收到某个消息,如:
User.should_receive(:all).once
Run Code Online (Sandbox Code Playgroud)
我怎么做?
UPD.通常,为模型和控制器编写测试,我们可以编写User.should_receive(:smth).once.但在我的情况下,我正在测试lib文件夹中的任意类,不知怎的,我总是收到以下消息:
<User( [fields] ) (class)> expected :all with (no args) once, but received it 0 times>
Run Code Online (Sandbox Code Playgroud)
关于为什么会这样的任何想法?测试以某种方式查看User类,但无法检查它是否收到消息.当然,我已经十次检查发现该用户是实际获得的消息.
我正在尝试在测试中使用模拟对象.但是,当我尝试列出模拟对象的期望时,抛出相同的异常.
以下是异常的堆栈跟踪:
java.lang.SecurityException: class "org.hamcrest.TypeSafeMatcher"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(Unknown Source)
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at …Run Code Online (Sandbox Code Playgroud) 考虑以下两个琐碎的模型:
class Iq
def score
#Some Irrelevant Code
end
end
class Person
def iq_score
Iq.new(self).score #error here
end
end
Run Code Online (Sandbox Code Playgroud)
以下Rspec测试:
describe "#iq_score" do
let(:person) { Person.new }
it "creates an instance of Iq with the person" do
Iq.should_receive(:new).with(person)
Iq.any_instance.stub(:score).and_return(100.0)
person.iq_score
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行此测试(或者更确切地说,类似的测试)时,看起来存根没有工作:
Failure/Error: person.iq_score
NoMethodError:
undefined method `iq_score' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
正如您可能猜到的那样,失败在上面标记为"错误"的行上.当该should_receive行被注释掉时,此错误消失.这是怎么回事?
我Expectations在测试用例中编写的块有问题:
new Expectations() {
{
mFindHandlerMock.findAll((Model) any, (Set<Id>) any, false);
if (!pWithRealData) {
result = Collections.emptySet();
} else {
result = pAllData;
}
times = 1;
Deencapsulation.invoke(mDb, "readSqlQuery", withAny(String.class));
result = "select * from realdata";
times = 1;
}
};
Run Code Online (Sandbox Code Playgroud)
测试用例崩溃:
java.lang.IllegalArgumentException: Invalid conditional statement inside expectation block
Run Code Online (Sandbox Code Playgroud)
正好在这里:
if (!pWithRealData) {
Run Code Online (Sandbox Code Playgroud)
它只是一个简单的boolean就是false在这种情况下.
我绝对不知道为什么exception会发生这种情况.我已经用谷歌搜索过但没有找到任何帮助.
你可以帮帮我吗?
error:- expected ';' at the end of the declaration list
Run Code Online (Sandbox Code Playgroud)
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
float number; error:- expected ';' at the end of the declaration list
float result;
int currentoperation;
__weak IBOutlet UILabel *label;
}
- (IBAction)canceloperation:(id)sender;
- (IBAction)cancelnumber:(id)sender;
- (IBAction)buttonoperation:(id)sender;
- (IBAction)buttonnumber:(id)sender;
@end
Run Code Online (Sandbox Code Playgroud)
请修复此代码.
expectations ×8
c++ ×2
googlemock ×2
java ×2
junit ×2
mocking ×2
rspec ×2
assert ×1
assertions ×1
class ×1
googletest ×1
jmockit ×1
message ×1
objective-c ×1
ruby ×1
stubbing ×1
testing ×1