Giter Site home page Giter Site logo

Comments (6)

kurenn avatar kurenn commented on July 29, 2024

Answering your questions

  1. Why we don't ever call our testable method #authenticate_with_token?
  2. It is probably my mistake, I'll be working on the second edition of the book and try to fix this kind of issues.
  3. As I understand, we only stubbing response object and check if stubbing was made perfecty? What is the sense?
  4. I'm stubbing method calls that may affect the final expectation, what do you think we should do?
  5. If I change this line
authentication.stub(:current_user).and_return(@user)

With this line they should not pass, are they passing?

from market_place_api.

fatkodima avatar fatkodima commented on July 29, 2024

4 If I write "#authenticate_with_token!" something like below, all tests should be of course passing. Are we really testing, that this method should respond to 401 and message "Not authenticated"?

module Authenticable

    def current_user
        @current_user ||= User.find_by(auth_token: request.headers['Authorization'])
    end

    def authenticate_with_token!
        render json: { ok: "You are welcome" },
                                status: :ok unless user_signed_in?
    end

    def user_signed_in?
        current_user.present?
    end

end

5 Yes, they are.

P.S. I find it as a better solution link:

  describe '.authenticate_with_token' do
    before do
      allow(authentication).to receive(:current_user).and_return(nil)
      allow(authentication).to receive(:render) do |args|
        args
      end
    end

    it 'returns error' do
      expect(authentication.authenticate_with_token![:json][:errors]).to eq 'Not authenticated'
    end

    it 'returns unauthorized status' do
      expect(authentication.authenticate_with_token![:status]).to eq :unauthorized
    end
  end

from market_place_api.

zoran avatar zoran commented on July 29, 2024

@fatkodima thanks for the link, this solution makes sense.

from market_place_api.

kurenn avatar kurenn commented on July 29, 2024

Thanks @fatkodima for the information, it will definitely going to be updated on the second edition of the book

from market_place_api.

danguyenwork avatar danguyenwork commented on July 29, 2024

@fatkodima @kurenn

Sorry to bring an old thread but can you tell me why?

    allow(authentication).to receive(:render) do |args| # возвращаем аргументы
          args
    end

is necessary? without it I get

undefined method "render"

from market_place_api.

kurenn avatar kurenn commented on July 29, 2024

It is just stubbing the render call, so when the actual call is made, you allow the method to receive that call and give an ouput to it

from market_place_api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.