localjumperror 没有给出块(产量) [英] localjumperror no block given (yield)

查看:31
本文介绍了localjumperror 没有给出块(产量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的规范时,它显示

When i ran my spec it shows

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

我就是这样调用服务的.

This is how I call the service.

UpdateManyPost.new.call do |response|
   puts(response)
end

我的 rspec 文件

My rspec file

describe 'call' do

   let(:posts) { build(:post, 3) }

   subject { UpdateManyPost.new }

   it "has to update all the post" do
     expect { subject.call } 
   end
end

当我运行规范时,它总是显示产量错误,我需要产量才能使其工作,但我不确定如何具体修复规范

When I ran the spec it always shows that yield error, I need the yield for it to work, but I'm not sure how to fix the spec specifically

推荐答案

因为你没有在测试中通过一个块

Since you are not passing a block in your test

expect { subject.call } 

你会得到一个让步错误,因为没有任何东西可以让步.

You will get a yield error because there is nothing to yield to.

您可以通过在该调用中传递一个块来解决这个问题,例如

You can solve this by passing a block in that call e.g.

expect { subject.call{|_|}} 

或者您可以更改方法定义以选择性地调用块

Or you can change your method definition to optionally call the block

def call
  posts.each do |post|
    response = UpdateAPost.new(post: post).call
    yield response if block_given? 
  end
end

这将检查是否给调用"一个块.方法和产量只有在给出一个块的情况下.

This will check if a block was given to the "call" method and yield only if a block was given.

也就是说,您的测试不会测试任何也会导致问题的内容,因为存在没有任何断言(匹配器)的期望.你想测试什么?

That being said your test does not test anything which will also cause issues because there is an expectation without any assertion (matcher). What are you trying to test?

你可以测试为

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 没有给出块(产量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆