title: How to test that a method is called when a block is given
body: <p>I got the task to write rspec tests for the following simplified method in case a block is given. The case when no block is given is already tested.</p>
  
  <pre><code>def make_something(name_id)
    name = find_name_by_id(name_id)
    if block_given? and name
      yield(something)
      name.save
    end
    name
  end
  </code></pre>
  
  <p>It's no problem to test if something got yielded, but I've got a problem to spec if name.save is beeing called. 
  I would do it like this:</p>
  
  <pre><code>let!(:name) { create(:name) }
  describe "when given a block" do
    #works
    it "yields something" do
      expect { |b| subject.make_something(name.id, &amp;b)}.to yield_with_args(something)
    end 
  
    #1. try: doesn't work
    it "saves the name" do
      expect(name).to receive(:save)
      subject.make_something(name.id) {}
    end
  
    #2. try: also doesn't work
    it "saves the name" do
      expect(name).to receive(:save)
      subject.make_something(name.id) {|b|}
    end
  end
  </code></pre>
  
  <p>I'm shure the solution is obvious but I just can't find it.</p>