localjumperror 没有给出块(yield)
localjumperror no block given (yield)
当我 运行 我的规格显示
localjumperror no block given (yield)
我的服务档案
service/update_many_post.rb
class UpdateManyPost
def call
posts.each do |post|
response = UpdateAPost.new(post: post).call
yield response
end
end
private
def posts
Post.all
end
end
/service/update_a_post.rb
class UpdateAPost
def initialize(post:)
@post = post
end
def call
@post.title = "I am great"
@post.save
end
end
我就是这样调用服务的。
UpdateManyPost.new.call do |response|
puts(response)
end
我的rspec文件
describe 'call' do
let(:posts) { build(:post, 3) }
subject { UpdateManyPost.new }
it "has to update all the post" do
expect { subject.call }
end
end
当我 运行 规范时,它总是显示产量错误,我需要产量才能工作,但我不确定如何具体修复规范
请查看您如何运行 规范中的 UpdateManyPost 服务
subject.call
方法“调用”等待传递给它的 lambda 表达式,但你没有传递任何东西
提供传输 lambda 以调用测试,一切正常。
例如:
expect do
subject.call do |it|
# you do something with it
end
end
因为您在测试中没有通过一个障碍
expect { subject.call }
你会得到一个屈服错误,因为没有任何东西可以屈服。
您可以通过在该调用中传递一个块来解决这个问题,例如
expect { subject.call{|_|}}
或者您可以更改方法定义以选择性地调用块
def call
posts.each do |post|
response = UpdateAPost.new(post: post).call
yield response if block_given?
end
end
这将检查是否为“call”方法提供了一个块,并且只有在提供了一个块时才会产生。
话虽如此,您的测试并未测试任何也会导致问题的内容,因为存在没有任何断言(匹配器)的期望。你想测试什么?
你可以测试为
subject.call do |resp|
expect(resp.saved_change_to_attribute?(:title)).to eq true
expect(resp.title).to eq("I am great")
end
或
expect(Post.where.not(title: "I am great").exists?).to eq true
subject.call
expect(Post.where.not(title: "I am great").exists?).to eq false
当我 运行 我的规格显示
localjumperror no block given (yield)
我的服务档案
service/update_many_post.rb
class UpdateManyPost
def call
posts.each do |post|
response = UpdateAPost.new(post: post).call
yield response
end
end
private
def posts
Post.all
end
end
/service/update_a_post.rb
class UpdateAPost
def initialize(post:)
@post = post
end
def call
@post.title = "I am great"
@post.save
end
end
我就是这样调用服务的。
UpdateManyPost.new.call do |response|
puts(response)
end
我的rspec文件
describe 'call' do
let(:posts) { build(:post, 3) }
subject { UpdateManyPost.new }
it "has to update all the post" do
expect { subject.call }
end
end
当我 运行 规范时,它总是显示产量错误,我需要产量才能工作,但我不确定如何具体修复规范
请查看您如何运行 规范中的 UpdateManyPost 服务
subject.call
方法“调用”等待传递给它的 lambda 表达式,但你没有传递任何东西
提供传输 lambda 以调用测试,一切正常。 例如:
expect do
subject.call do |it|
# you do something with it
end
end
因为您在测试中没有通过一个障碍
expect { subject.call }
你会得到一个屈服错误,因为没有任何东西可以屈服。
您可以通过在该调用中传递一个块来解决这个问题,例如
expect { subject.call{|_|}}
或者您可以更改方法定义以选择性地调用块
def call
posts.each do |post|
response = UpdateAPost.new(post: post).call
yield response if block_given?
end
end
这将检查是否为“call”方法提供了一个块,并且只有在提供了一个块时才会产生。
话虽如此,您的测试并未测试任何也会导致问题的内容,因为存在没有任何断言(匹配器)的期望。你想测试什么?
你可以测试为
subject.call do |resp|
expect(resp.saved_change_to_attribute?(:title)).to eq true
expect(resp.title).to eq("I am great")
end
或
expect(Post.where.not(title: "I am great").exists?).to eq true
subject.call
expect(Post.where.not(title: "I am great").exists?).to eq false