加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 失败/错误:预期:任何争论1次,收到:任何参数

发布时间:2020-12-17 01:33:55 所属栏目:百科 来源:网络整理
导读:我有一个下面的规范,我在嘲笑我的用户模型和存根其方法. require 'spec_helper'describe User do let(:username) {"test@test.com"} let(:password) {"123"} let(:code) {"0"} context "when signing in" do let(:expected_results) { {token:"123"}.to_json
我有一个下面的规范,我在嘲笑我的用户模型和存根其方法.

require 'spec_helper'

describe User do 

  let(:username) {"test@test.com"}
  let(:password) {"123"}
  let(:code) {"0"}

  context "when signing in" do
    let(:expected_results) { {token:"123"}.to_json }

    it "should sign in" do 
      expect(User).to receive(:login).with({email: username,password: password,code: code})
        .and_return(expected_results)
    end

  end    
end

当我尝试运行我的测试用例时,我得到以下错误.

Failure/Error: expect(User).to receive(:login).with({email: username,code: code})
       (<User (class)>).login({:email=>"test@test.com",:password=>"123",:code=>"0"})
           expected: 1 time with arguments: ({:email=>"test@test.com",:code=>"0"})
           received: 0 times

解决方法

你误解了期望是什么.

expect(x).接收(:y)是对x的y方法进行删除.

也就是说,它是关于该方法的论文.

一种描述这种方式的方法是,您正在“期望在实际运行代码时将在x上调用方法y”

现在,你的规范没有调用任何实际的代码…它只是设置了期望…然后停止.

如果你正在测试方法登录,那么你需要不要期望它,但实际上是真实的.

例如User.login(电子邮件:用户名,密码:密码,代码:代码)

你目前根本没有测试.只是你设置的存根,然后永远不会使用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读