2014-12-23 48 views
0

我刚添加了一个新功能到我的模型,并希望用rspec进行测试。似乎我做错了什么,因为我的测试保持失败,没有任何东西被存储在分贝。我想要的是作为一个用户阻止另一个用户。rspec测试不保存分贝

我的用户模型有以下几点:

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

我user_controller有以下几点:

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 

我阻止模式有以下几点:

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 

,这是我的测试:

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 

我想测试user_two是否被user_one阻止。既没有存储在数据库中也没有。任何帮助?

这就是我得到后,我excecute测试:

expected: #<User id: 2, email: "[email protected]", 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:[email protected]/Users/toptierlabs/Documents/projects/kinnecting_backend/spec/controllers/api/users_controller_spec.rb:206> 

回答

0

你传递一个块expect,其目的是在情况下使用,你要评估该块是如何执行改变环境(例如通过to_change)。它通常在一个事务的上下文中执行,但在你的情况下,它并没有被执行,因为你只是将它与eq匹配器一起使用。

如果您想查询由控制器操作的返回值,你需要检查的response价值为:

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) 

更多关于此见How to check for a JSON response using RSpec?