stubbing

Stubbing with Faraday and Rspec

僤鯓⒐⒋嵵緔 提交于 2019-12-12 07:31:56
问题 I have a model that looks like this: class Gist def self.create(options) post_response = Faraday.post do |request| request.url 'https://api.github.com/gists' request.headers['Authorization'] = "Basic " + Base64.encode64("#{GITHUB_USERNAME}:#{GITHUB_PASSWORD}") request.body = options.to_json end end end and a test that looks like this: require 'spec_helper' describe Gist do context '.create' do it 'POSTs a new Gist to the user\'s account' do Faraday.should_receive(:post) Gist.create({:public =

RSpec: how to test Rails logger message expectations?

不羁岁月 提交于 2019-12-12 07:09:15
问题 I am trying to test that the Rails logger receives messages in some of my specs. I am using the Logging gem. Let's say that I have a class like this: class BaseWorker def execute logger.info 'Starting the worker...' end end And a spec like: describe BaseWorker do it 'should log an info message' do base_worker = BaseWorker.new logger_mock = double('Logging::Rails').as_null_object Logging::Rails.stub_chain(:logger, :info).and_return(logger_mock) logger_mock.should_receive(:info).with('Starting

Unable to create stub with Array argument in ScalMock

a 夏天 提交于 2019-12-12 04:49:40
问题 Here is an example of what I try to achieve. Stub always retuns null, but if I change Array(1L) to * it works. It seems there is a problem with array arguments. trait Repo { def getState(IDs: Array[Long]): String } "test" should "pass" in { val repo = stub[Repo] (repo.getState _).when(Array(1L)).returns("OK") val result = repo.getState(Array(1L)) assert(result == "OK") } 回答1: See this post: Why doesn't Array's == function return true for Array(1,2) == Array(1,2)? ScalaMock is working fine,

RSpec - mock (or stub) overriden mixin method

时光怂恿深爱的人放手 提交于 2019-12-10 21:05:06
问题 I have situaltion like this: module Something def my_method return :some_symbol end end class MyClass include Something def my_method if xxx? :other_symbol else super end end end Now the problem is with testing - I would like to ensure that super method got called from overriden method and stub it so that I can test other parts of method. How can I accomplish that using RSpec mocks? 回答1: Ensuring that super gets called sounds a lot like testing the implementation, not the behaviour, and

Given wsdl + xds type file, how do I create a stub WCF webservice?

五迷三道 提交于 2019-12-09 11:29:41
问题 I understand this is a basic topic but never done this before starting from wsdl. I am being handed a wsdl file and a bunch of xsd with the types definitions. I don't have a clue if they were created from a WCF service (I guess so because of the split out format) but I do need to create a WCF service that implements the contract. Question: How do I get the service contract interface? I know about wsdl.exe and svcutil.exe - but not too familiar with what's what. I guess after that all that's

How do I stub out a current user's attributes in a view spec

强颜欢笑 提交于 2019-12-06 16:00:04
问题 I have a view spec where I'm testing conditional output. How do I get the spec to return the user I've mocked out? View file : .content - if @current_user.is_welcome == true Welcome to the site View spec : before(:each) do @user = mock_model(User) @user.stub!(:is_welcome).and_return(true) view.stub(:current_user).and_return(@user) end it "show content" do #assign(:current_user, stub_model(User, dismiss_intro: true)) render rendered.should have_content("Welcome to the site") end Running the

Should I stub the model in Factory girl or in the spec file while testing?

≯℡__Kan透↙ 提交于 2019-12-05 12:17:04
问题 Almost every spec file I come accross I end up writing stuff like: before :each do @cimg = Factory.build :cimg_valid @cimg.stub(:validate_img).and_return true @cimg.stub(:validate_img_url).and_return true @cimg.stub(:save_images).and_return true @cimg.stub(:process_image).and_return true @cimg.stub(:img).and_return true end I mean, the model I get from Factory.build is completely valid. But if I don't stub that stuff it saves things in the filesystem, and validates stuff I'm not testing...

Is there a way to stub a method of an included module with Rspec?

本秂侑毒 提交于 2019-12-05 08:40:03
问题 I have a module that is included in another module, and they both implement the same method. I would like to stub the method of the included module, something like this: module M def foo :M end end module A class << self include M def foo super end end end describe "trying to stub the included method" do before { allow(M).to receive(:foo).and_return(:bar) } it "should be stubbed when calling M" do expect(M.foo).to eq :bar end it "should be stubbed when calling A" do expect(A.foo).to eq :bar

Stub out address geocoding during RSpec unit test

喜欢而已 提交于 2019-12-05 03:25:21
I'm using the geocoder gem to add geocoding functionality to one of my Active Record model classes. This works great, but I don't actually want the geocoding to fire during unit tests. I've tried stubbing out the call to geocode by adding this to my RSpec test: before(:each) do User.stub!(:geocode).and_return([1,1]) end However, when I run my tests it still appears to be calling out to geocode. What am I doing wrong? FYI, this all works if I stub on the instance level (e.g. some_user.stub! instead of User.stub!). If you want to use stubbing on the instance level, you should use other mocking

How do I stub out a current user's attributes in a view spec

Deadly 提交于 2019-12-04 20:42:31
I have a view spec where I'm testing conditional output. How do I get the spec to return the user I've mocked out? View file : .content - if @current_user.is_welcome == true Welcome to the site View spec : before(:each) do @user = mock_model(User) @user.stub!(:is_welcome).and_return(true) view.stub(:current_user).and_return(@user) end it "show content" do #assign(:current_user, stub_model(User, dismiss_intro: true)) render rendered.should have_content("Welcome to the site") end Running the spec returns undefined method is_welcome for nil:NilClass You have stubbed the method named current_user