【问题标题】:How to stub model method for rspec controller test如何为 rspec 控制器测试存根模型方法
【发布时间】:2017-05-23 05:30:39
【问题描述】:

我正在尝试对我在控制器中使用的模型方法进行存根,但它似乎永远无法正常工作。有人可以告诉我正确的方法吗

用户控制器

 if current_user.user_token
      @user = @account.users.find(params[:id])
      @user.revoke_seat(:admin, current_user)
      render :template => "/admin/users/revoke_seat"
    else
      render :js => "window.location.href='#{server_url}/oauth/authorize?response_type=code&client_id=#{client_id}&state=#{request.referrer}?auto_revoke_seat=true&redirect_uri=#{auth_service_callback_url}";
    end

Rspec

 before do
    users(:admin).stub(:internal_admin?).and_return(true)
    login_as :admin
    user.stub(:user_token).and_return("123123")# THIS IS NOT WORKING
  end

  it "should redirect to authentication service to generate access token" do
      expect(user).to receive(:user_token).and_return(true)
      xhr :put, :revoke_seat, account_id: account.id, id: user.id
      expect(response).to render_template('admin/users/revoke_seat')
      expect(assigns(:account)).to eq(account)
      expect(assigns(:user)).to eq(user)
    end

【问题讨论】:

    标签: ruby-on-rails-3 rspec rspec-rails


    【解决方案1】:

    您可以尝试allow 方法而不是stub。例如,allow(:admin).to receive(:internal_admin?).and_return(true)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-04
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多