How do you check if a model has this method in Rails?

I would like to implement a method User.calculate_hashed_password. I am trying to use the Shoulda testing library, which works with the built-in Rails testing tools, so the answer related to Test :: Unit will be as good as the one related to Shoulda (I think).

I'm trying to figure out what I need to check, and how , I have to test it. My initial idea is to do something like ...

class UserTest < ActiveSupport::TestCase
  should 'Return a hashed password'
    assert_not_nil User.calculate_hashed_password
  end
end

Is this right to do?

+3
source share
4 answers

, , . :

class UserTest < ActiveSupport::TestCase
  setup do
    @user = User.new
  end

  should 'Calculate the hashed password correctly'
    @user.password = "password"
    @user.hashed_password = "xxxxx" # Manually calculate it
  end
end

( shoulda, .)

, .

+8

; , dylanfm, #respond_to RSpec.

it "should know about associated Projects" do
  @user.should respond_to(:projects)
end
+5

, respond_to?

+3
0

Source: https://habr.com/ru/post/1702509/


All Articles