rspec-mocks

How do I test the rescue block of a method with rspec mocks 3.3

心已入冬 提交于 2020-06-28 08:21:28
问题 Help me make this test pass: Here is an example of some rspec code, class User attr_accessor :count def initialize @count = 0 end # sometimes raises def danger puts "IO can be dangerous..." rescue IOError => e @count += 1 end #always raises def danger! raise IOError.new rescue IOError => e @count += 1 end end describe User do describe "#danger!" do it "its rescue block always increases the counter by one" do allow(subject).to receive(:'danger!') expect { subject.danger! }.to change(subject,

Rspec/mocks raises uninitialized constant BasicObject::RSpec

橙三吉。 提交于 2020-01-15 03:28:09
问题 I just installed rspec and rspec-mocks but I am not able to run the simplest setup irb(main):001:0> require 'rspec' => true irb(main):004:0> require 'rspec/mocks' => false irb(main):006:0> RSpec::Mocks::setup(Object.new) NameError: uninitialized constant BasicObject::RSpec from /usr/local/Cellar/ruby/1.9.1-p376/lib/ruby/gems/1.9.1/gems/rspec-mocks-2.13.0/lib/rspec/mocks.rb:60:in `block in add_extensions' from /usr/local/Cellar/ruby/1.9.1-p376/lib/ruby/gems/1.9.1/gems/rspec-mocks-2.13.0/lib

How to stub a class method using rspec/rspec-mocks

旧街凉风 提交于 2020-01-01 11:49:29
问题 I am using rspec-mock for test-driven-development. I am starting implementing a single class and mocking/stubbing the other classes using rspec-mock. Mocking objects of classes yet to be implemented works well. However when I try to mock a class method of a class that does not exist yet, I haven't been successful. My class "Hashes" should have a class method "calculate_hashes" receiving a filename and returning a hash. I tried allow(Hashes).to receive(:calculate_hash) do |file| # looks up

How to stub a class method using rspec/rspec-mocks

只谈情不闲聊 提交于 2019-12-04 07:02:38
I am using rspec-mock for test-driven-development. I am starting implementing a single class and mocking/stubbing the other classes using rspec-mock. Mocking objects of classes yet to be implemented works well. However when I try to mock a class method of a class that does not exist yet, I haven't been successful. My class "Hashes" should have a class method "calculate_hashes" receiving a filename and returning a hash. I tried allow(Hashes).to receive(:calculate_hash) do |file| # looks up what to return end which give the error "Hashes is not a class". I then implemented a class "Hashes" class

RSpec Mock Object Example

随声附和 提交于 2019-12-03 01:39:43
问题 I am new to mock objects, and I am trying to learn how to use them in RSpec. Can someone please post an example (a hello RSpec Mock object world type example), or a link (or any other reference) on how to use the RSpec mock object API? 回答1: Here's an example of a simple mock I did for a controller test in a rails application: before(:each) do @page = mock_model(Page) @page.stub!(:path) @page.stub!(:find_by_id) @page_type = mock_model(PageType) @page_type.stub!(:name) @page.stub!(:page_type)

RSpec Mock Object Example

我的梦境 提交于 2019-12-02 15:08:53
I am new to mock objects, and I am trying to learn how to use them in RSpec. Can someone please post an example (a hello RSpec Mock object world type example), or a link (or any other reference) on how to use the RSpec mock object API? Here's an example of a simple mock I did for a controller test in a rails application: before(:each) do @page = mock_model(Page) @page.stub!(:path) @page.stub!(:find_by_id) @page_type = mock_model(PageType) @page_type.stub!(:name) @page.stub!(:page_type).and_return(@page_type) end In this case, I'm mocking the Page & PageType models (Objects) as well as stubbing