Ruby on Rails中,为何参数传递错误,预期一次却未收到任何参数?

2026-04-11 15:371阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计394个文字,预计阅读时间需要2分钟。

Ruby on Rails中,为何参数传递错误,预期一次却未收到任何参数?

我有一套底层的规范,我在编写用户模型和存储方式时,遵循这些规范。+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) { '...' } end end

Ruby on Rails中,为何参数传递错误,预期一次却未收到任何参数?

我有一个下面的规范,我在嘲笑我的用户模型和存根其方法.

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, password: password, code: code}) (<User (class)>).login({:email=>"test@test.com", :password=>"123", :code=>"0"}) expected: 1 time with arguments: ({:email=>"test@test.com", :password=>"123", :code=>"0"}) received: 0 times 你误解了期望是什么.

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

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

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

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

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

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

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

本文共计394个文字,预计阅读时间需要2分钟。

Ruby on Rails中,为何参数传递错误,预期一次却未收到任何参数?

我有一套底层的规范,我在编写用户模型和存储方式时,遵循这些规范。+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) { '...' } end end

Ruby on Rails中,为何参数传递错误,预期一次却未收到任何参数?

我有一个下面的规范,我在嘲笑我的用户模型和存根其方法.

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, password: password, code: code}) (<User (class)>).login({:email=>"test@test.com", :password=>"123", :code=>"0"}) expected: 1 time with arguments: ({:email=>"test@test.com", :password=>"123", :code=>"0"}) received: 0 times 你误解了期望是什么.

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

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

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

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

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

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

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