testing with rspec codeschool level 5 challenge 4

一世执手 提交于 2019-12-25 01:45:41

问题


Here is the base question for the test:

We've changed the code below so we're mocking ZombieMailer.tweet method instead of the entire method. There is still more to do to make this example work. First, finish the let(:mail) statement below by creating a stub with a deliver method that returns true.
Then update the mock so the call to the tweet method returns the mail stub you created. 

Unfortunately I can not alter the models, question, or mailer.

Models:

# tweet.rb
class Tweet < ActiveRecord::Base
  belongs_to :zombie
  validates :message, presence: true
  attr_accessible :message

  after_create :email_tweeter

  def email_tweeter
    ZombieMailer.tweet(zombie, self).deliver
  end
  private :email_tweeter
end

# zombie.rb
class Zombie < ActiveRecord::Base
  has_many :tweets
  validates :email, presence: true
  attr_accessible :email
end

Mailer:

class ZombieMailer < ActionMailer::Base
  def tweet(zombie, tweet)
    mail(:from => 'admin@codeschool.com',
         :to => zombie.email,
         :subject => tweet.message)
  end
end

I keep bouncing around on this and could use a few pointers. Here is what I have been working with now that I got past test question 3. Now they are asking to add the deliver method, but his method does not exist, or at least I am not placing it in the correct place.

UPDATED

describe Tweet do
  context 'after create' do
    let(:zombie) { Zombie.create(email: 'anything@example.org') }
    let(:tweet) { zombie.tweets.new(message: 'Arrrrgggghhhh') }
    let(:mail) { stub(:deliver => true) }

    it 'calls "tweet" on the ZombieMailer' do
      ZombieMailer.should_receive(:tweet).returns(:mail)
      tweet.save
    end
  end
end

And the error message is:

Failures:

1) Tweet after create calls "tweet" on the ZombieMailer
Failure/Error: ZombieMailer.should_receive(:tweet).returns(:mail)
NoMethodError:
undefined method `returns' for #<RSpec::Mocks::MessageExpectation:0x0000000519ab90>
# zombie_spec.rb:8:in `block (3 levels) '

Finished in 0.37725 seconds
1 example, 1 failure

Failed examples:

rspec zombie_spec.rb:7 # Tweet after create calls "tweet" on the ZombieMailer

Any rspec peeps out there can point me in the right direction as to what I am missing here? Thank you.


回答1:


Thank you to @Paritosh and @Alex from the comments above, here is the final answer.

describe Tweet do
  context 'after create' do
    let(:zombie) { Zombie.create(email: 'anything@example.org') }
    let(:tweet) { zombie.tweets.new(message: 'Arrrrgggghhhh') }
     let(:mail) { stub(:deliver => true) }

    it 'calls "tweet" on the ZombieMailer' do
      ZombieMailer.should_receive(:tweet).and_return(mail)
      tweet.save
    end
  end
end


来源:https://stackoverflow.com/questions/22887106/testing-with-rspec-codeschool-level-5-challenge-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!