使用 rspec Codeschool 5 级挑战 4 进行测试 [英] testing with rspec codeschool level 5 challenge 4

查看:31
本文介绍了使用 rspec Codeschool 5 级挑战 4 进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是测试的基本问题:

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. 

很遗憾,我无法更改模型、问题或邮件.

型号:

# 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

邮递员:

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

我一直在讨论这个问题,可以使用一些指针.这是我通过测试问题 3 后一直在使用的内容.现在他们要求添加交付方法,但他的方法不存在,或者至少我没有将它放在正确的位置.

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.

更新

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

错误信息是:

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

任何 rspec 窥视都可以指出我在这里缺少什么的正确方向?谢谢.

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

推荐答案

感谢@Paritosh 和@Alex 以上评论,这里是最终答案.

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

这篇关于使用 rspec Codeschool 5 级挑战 4 进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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