小编Bob*_*Bob的帖子

Jasmine spyOn有多个回报

我想用Jasmine测试我的角度应用程序.所以我创建了一些测试,其中大部分工作正常.但是,我的一个功能要求用户填写提示.测试无法填写此提示,因此我嘲笑他们spyOn(window,'prompt').and.returnValue('test').这有效,但只有一次.

当我添加两个组件(提示所在的函数)时,我希望spyOn第一个提示结果为'test',第二个提示符为'test2'.我尝试这样做如下:

it 'should place the component as last object in the form', ->

      spyOn(window, 'prompt').and.returnValue('test')
      builder.addFormObject 'default', {component: 'test'}

      spyOn(window, 'prompt').and.returnValue('test2')
      builder.addFormObject 'default', {component: 'test2'}

      expect(builder.forms['default'][0].name).toEqual('test')
Run Code Online (Sandbox Code Playgroud)

但这会产生以下错误:Error: prompt has already been spied upon 这是非常合乎逻辑的,但我不知道用spyOn返回的另一种方式.

所以,我想要的是:在第一个addFormObject之前,我想监视返回'test'的提示符.第二个addFormObject我想监视返回'test2'

javascript coffeescript jasmine angularjs

7
推荐指数
2
解决办法
3004
查看次数

如何在蛋糕3中创建自定义验证规则

我正在开发Cake 3.我想创建一个自定义验证规则.我想检查字段'password'是否等于'confirm_password'字段.

这是我的代码:

 public function validationDefault(Validator $validator) {
        $validator
                ->add('id', 'valid', ['rule' => 'numeric'])
                ->allowEmpty('id', 'create')
                ->add('email', 'valid', ['rule' => 'email'])
                ->requirePresence('email', 'create')
                ->notEmpty('email')
                ->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table'])
                ->requirePresence('password', 'create')
                ->notEmpty('password')
                ->notEmpty('confirm_password')
                ->add('confirm_password', 'custom', [
                    'rule' => function($value, $context) {
                        if ($value !== $context['data']['password']) {
                            return false;
                        }
                        return false;
                    },
                    'message' => 'The passwords are not equal',
        ]);

        return $validator;
    }
Run Code Online (Sandbox Code Playgroud)

当我尝试'失败'表单提交时,代码保存,我没有错误.

我阅读http://book.cakephp.org/3.0/en/core-libraries/validation.html#custom-validation-rules但没有帮助....任何人?

谢谢!

cakephp-3.0

6
推荐指数
2
解决办法
1万
查看次数

CakePHP 3 - beforeSave回调无法编辑

我有一个beforeSave-callback,每当我创建一个新实体时都会被调用.但是当我编辑时,它根本就没有被调用.在文档中找不到任何可能有用的内容.

这是我的编辑功能:

public function edit($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid article'));
    }

    $article = $this->Articles->get($id);
    if ($this->request->is(['post', 'put'])) {
        $this->Articles->patchEntity($article, $this->request->data);
        if ($this->Articles->save($article)) {
            $this->Flash->success(__('Your article has been updated.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to update your article.'));
    }

    $this->set('article', $article);
} 
Run Code Online (Sandbox Code Playgroud)

cakephp edit callback cakephp-3.0

3
推荐指数
1
解决办法
4445
查看次数