1

I just added a new function to my model and wanted to tested with rspec. It seems as if i'm doing something wrong because my test keep failing and nothing is being stored at the db. What I want is to have the hability to, as a user, block another user.

My user model has the following:

  has_many :blockeds
  has_many :blocked_users, :through=> :blockeds

My user_controller has the following:

def block
      block_action = Blocked.new
      block_action.add_blocked(current_user.id,params[:id])
      current_user.blockeds << User.find(params[:id])
    end

    def is_blocked
      blocked = current_user.blocked_by(current_user.id,params[:id])
      blocked
    end

my Blocked model has the following:

belongs_to :user_blocking, class_name: 'User'
  belongs_to :user_blocked, class_name: 'User'

  def add_blocked(blocking_id,blocked_id)
    self.user_blocking_id = blocking_id
    self.user_blocked_id = blocked_id
    self.save!
  end

and this is my test:

describe 'Block' do

    let(:user_one) { Fabricate :user }
    let(:user_two) { Fabricate :user }

    it 'should block a user' do
      post :block, current_user: user_one.to_param, id: user_two.id.to_param, format: :json
      expect{
        post :is_blocked, current_user: user_one.to_param, id: user_two.id.to_param, format: :json
      }.to eq(user_two)
    end
  end

I wanted to test if user_two was blocked by user_one. Nothing being stored at the db neither. any help?

This is what i get after i excecute the test:

expected: #<User id: 2, email: "arden@hotmail.com", encrypted_password: "$2a$04$cKnZx8h9nVX1xQOruH6.yeSHIl989EA.amK.fqz4kwz...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2014-12-23 13:48:32", updated_at: "2014-12-23 13:48:32", bio: nil, fb_access_token: "accusamuscumsit", fb_app_id: "essesedmaiores", phone: nil, address: nil, authentication_token: "vH3N1KTz1AVmP8fTRAye", gender: "male", profile_completed: false, zip_code: "95304-2111", state: "Indiana", city: "New Chaunceymouth", latitude: 37.6841772, longitude: -121.3770336, access_code_id: nil, locked_at: nil, cover: nil, fb_global_id: nil, birthday: "1996-02-18", age: 226, channel_id: "mh3_dPdbihISTdX8TCOKkQ", first_name: "Tressa", last_name: "Keeling", access_code_type: nil, facebook_data_updated_at: nil>
            got: #<Proc:0x00000107fafd68@/Users/toptierlabs/Documents/projects/kinnecting_backend/spec/controllers/api/users_controller_spec.rb:206>
Jude
  • 47
  • 5

1 Answers1

0

You're passing a block to expect, which is intended to be used in situations where you want to evaluate how the execution of that block changes the environment (e.g. via to_change). It's typically executed in the context of a transaction, but in your case it's not being executed at all since you're just using it with the eq matcher.

If you want to check the value returned by a controller operation, you need to check the value of response as in:

post :is_blocked, current_user: user_one.to_param, id: user_two.id.to_param, format: :json
expect(response.body).to eq(user_two.to_json)

See How to check for a JSON response using RSpec? for more on this.

Community
  • 1
  • 1
Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106