ruby-on-rails – 不能使用Rspec来存储东西

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 不能使用Rspec来存储东西前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Rails 4应用程序,这里是我的lib / foobar:
  1. jan@rmbp ~/D/r/v/l/foobar> tree
  2. .
  3. ├── foo_bar.rb
  4. └── foobar_spec.rb
  5.  
  6. 0 directories,2 files

文件

foob​​ar_spec.rb

  1. require "spec_helper"
  2.  
  3. describe "FooBar" do
  4. subject { FooBar.new }
  5. its(:foo) { should == "foo"}
  6. #stubbed version of test crashes
  7. #FooBar.stub(:foo).and_return("bar")
  8. #subject { FooBar.new }
  9. #its(:foo) { should == "bar"}
  10.  
  11. end

foo_bar.rb

  1. class FooBar
  2. def foo
  3. "foo"
  4. end
  5. end

spec_helper.rb:

  1. # This file is copied to spec/ when you run 'rails generate rspec:install'
  2. ENV["RAILS_ENV"] ||= 'test'
  3. require File.expand_path("../../config/environment",__FILE__)
  4. require 'rspec/rails'
  5. # commented for zeus two runs bug
  6. require 'rspec/autorun'
  7. require 'capybara/rspec'
  8.  
  9. # Requires supporting ruby files with custom matchers and macros,etc,# in spec/support/ and its subdirectories.
  10. Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
  11.  
  12. # Checks for pending migrations before tests are run.
  13. # If you are not using ActiveRecord,you can remove this line.
  14. ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
  15.  
  16. RSpec.configure do |config|
  17.  
  18. config.include Devise::TestHelpers,:type => :controller
  19. config.include Features::SessionHelpers,type: :feature
  20.  
  21. # ## Mock Framework
  22. #
  23. # If you prefer to use mocha,flexmock or RR,uncomment the appropriate line:
  24. #
  25. # config.mock_with :mocha
  26. # config.mock_with :flexmock
  27. # config.mock_with :rr
  28. config.mock_with :rspec
  29.  
  30. # If you're not using ActiveRecord,or you'd prefer not to run each of your
  31. # examples within a transaction,remove the following line or assign false
  32. # instead of true.
  33. config.use_transactional_fixtures = false
  34.  
  35. config.before(:suite) do
  36. DatabaseCleaner.strategy = :truncation
  37. end
  38.  
  39. config.before(:each) do
  40. DatabaseCleaner.start
  41. end
  42.  
  43. config.after(:each) do
  44. DatabaseCleaner.clean
  45. end
  46.  
  47. # If true,the base class of anonymous controllers will be inferred
  48. # automatically. This will be the default behavior in future versions of
  49. # rspec-rails.
  50. config.infer_base_class_for_anonymous_controllers = false
  51.  
  52. # Run specs in random order to surface order dependencies. If you find an
  53. # order dependency and want to debug it,you can fix the order by providing
  54. # the seed,which is printed after each run.
  55. # --seed 1234
  56. config.order = "random"
  57. end

规格通过罚款.但是当我取消注释这一行:

  1. # FooBar.stub(:foo).and_return("bar")

它失败了:

  1. /Users/jan/.rvm/gems/ruby-2.0.0-p247/gems/rspec-mocks-2.14.3/lib/rspec/mocks.rb:26:in `proxy_for': undefined method `proxy_for' for nil:NilClass (NoMethodError)

哪里不对?

编辑:
此外,我似乎也无法使用webmock.

  1. stub_request(:post,"https://accounts.google.com/o/oauth2/token")
  2. .with(:body => { "client_id" => CLIENT_ID,"client_secret" => CLIENT_SECRET,"refresh_token" => refresh_token,}
  3. ).to_return(:status => 200,:body => File.read("#{$fixtures}/refresh_token.json"))

返回:

/Users/jan/Documents/ruby/vince.re/lib/youtube/you_tube_test.rb:9:in
block in <top (required)>': undefined methodstub_request’ for
< Class :0x007f8159bbe7c0> (NoMethodError)

解:
感谢@gotva告诉我关于stubs要求驻留在其中.这是我的新的,固定的webmock测试,它的工作原理很好:

  1. context "when token is nil it" do
  2. it "called refresh method" do
  3. YouTube.any_instance.should_receive(:refresh_auth_token).with(data["refresh"])
  4. YouTube.new(data["uid"],nil,data["refresh"])
  5. end
  6. it "refreshed the authentation token" do
  7. stub_request(:post,"https://accounts.google.com/o/oauth2/token")
  8. .with(:body => { "client_id" => CLIENT_ID,"grant_type"=>"refresh_token","refresh_token" => data["refresh"],}
  9. ).to_return(:status => 200,:body => File.read("#{$fixtures}/refresh_token.json"))
  10. yt = YouTube.new(data["uid"],data["refresh"])
  11. yt.token.should == data["access_token"]
  12. end
  13. end

解决方法

在我看来,使用这个存根
  1. FooBar.stub(:foo).and_return("bar")

您正在尝试存根不存在的方法.
方法foo是实例方法,但是你存根类方法.

如果你想存根实例方法FooBar#foo使用any_instance

  1. FooBar.any_instance.stub(:foo).and_return("bar")

评论更新

在其中或在块之前应用桩.

新变体:

  1. it 'place here some description for method foo' do
  2. FooBar.any_instance.stub(:foo).and_return("bar")
  3. expect(FooBar.new.foo).to eql('bar')
  4. end

要么

  1. # the order is important!
  2. describe "FooBar" do
  3. before { FooBar.any_instance.stub(:foo).and_return("bar") }
  4. subject { FooBar.new }
  5. its(:foo) { should == "foo"}
  6. end

猜你在找的Ruby相关文章