我想在 Laravel 中为 API 调用编写测试。我创建了一个模型工厂来为我的测试数据库设置种子,以便 API 调用可以返回一些东西。在模型工厂内部,我使用 Faker 为模型属性生成随机数据。我在测试开始时使用这个工厂,所以数据是在运行时创建的。
如何测试 API 调用的结果?问题是在每次执行测试期间,由于随机生成的数据,API 调用可能返回不同的结果。所以我不能只是将 API 调用的结果保存在 .json 文件中,并将其与测试中 API 调用的实际结果进行比较。
我知道该seeJsonStructure()
方法(https://laravel.com/docs/5.3/application-testing#verifying-structural-match)但我不想使用它。
所以这是我目前的尝试,但不起作用:
$cars = factory(Car::class)->times(2)->create();
$this->json('GET', '/api/cars');
$expected = File::get('cars.json');
$this->seeJson((array) json_decode($expected));
Run Code Online (Sandbox Code Playgroud) 我快要绝望了!我需要从指向我的基类的指针调用非虚拟成员函数:
class A { };
class B : public A { public: void evilFunction() { std::cout << "Yay!"; } };
int main(void) {
A *pointer = new B();
// Now do something like this:
// pointer->evilFunction();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以用dynamic_cast做到这一点 - 但我不被允许!我真的不知道我还能做些什么.从理论上讲,由于我有指针,我很确定我可以用指针算术做一些魔法来获取函数的内存位置然后调用它,但我不知道怎么做或者至少如何开始.
谁能给我一个提示?这就是我所需要的一切.或者我将使用我的史诗初学者技能编写代码来删除g ++以报复C++对我造成的痛苦!你不能让这种情况发生,对吧?!
初学者问题:如何获取指针的地址并将其保存为int?例:
int *ptr = xyz;
int i = static_cast<int>(ptr);
Run Code Online (Sandbox Code Playgroud)
因此,如果ptr指向内存地址123,我应该是123.我的编译器说它是一个error: invalid static_cast from type 'int*' to type 'int'
.
猜猜我错过了什么,但我不知道是什么.
我有一个带有'里面的字符串:
<a href="#" onclick="if (confirm('... ' ...')) { document.exampleFormName.submit(); }">example link text</a>
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎不起作用.Firebug在参数列表之后说"SyntaxError:missing",你可以看到HTML实体已经被'替换了'.
我该怎么做才能避免这个问题?