带有更改方法的rspec返回错误的参数数量0表示1

时间:2011-02-02 01:01:37

标签: ruby-on-rails ruby-on-rails-3 rspec factory-bot

我正在尝试这个测试。

模型

def self.tweet(url)
  Twitter.configure do |config|
    config.consumer_key = APP_CONFIG['twitter_consumer_key']
    config.consumer_secret = APP_CONFIG['twitter_consumer_secret']
    config.oauth_token = APP_CONFIG['twitter_access_token']
    config.oauth_token_secret = APP_CONFIG['twitter_secret_token']
  end    
  shorted_url = shorten_url(url)
  Twitter.update("#{title} - #{shorted_url}")
end

def self.shorten_url(url)
  authorize = UrlShortener::Authorize.new APP_CONFIG['bit_ly_id'], APP_CONFIG['bit_ly_api_key']
  client = UrlShortener::Client.new authorize
  shorten_url = client.shorten(url).urls
end

def publish(url)
  update_attributes(:available => true, :locked => false)
  tweet(url)
end

现在,我试图测试最后一个方法,“发布”但是我得到错误的参数数量错误信息。

在测试方面,我有这个:

describe Job, "publish" do

  it "should publish a job" do
    @job = Factory(:job)
    @job.publish.should change(Job.available).from(false).to(true)
  end

end

和错误消息:

1) Job Job publish should publish a job
     Failure/Error: @job.publish.should change(Job.available).from(false).to(true)
     wrong number of arguments (0 for 1)
     # ./app/models/job.rb:60:in `publish'
     # ./spec/models/job_spec.rb:128:in `block (3 levels) in <top (required)>'

Finished in 1.12 seconds
47 examples, 1 failure, 4 pending

感谢任何帮助!

谢谢!

1 个答案:

答案 0 :(得分:3)

尝试将其放入lambda:

lambda { @job.publish }.should change(Job.available).from(false).to(true)

此外,我不确定,因为我无法看到您所有的模型代码,但您的意思是使用@job.available代替Job.available吗?

修改:您可能需要使用以下格式:

lambda { @job.publish }.should change(@job, :available).from(false).to(true)