rspec为没有工厂女孩的控制器

时间:2018-05-23 06:07:30

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

我正在尝试为我的控制器编写规范代码,但它失败了。而且我不确定失败的地方。

控制器代码

def index
    @users = User.all
  end

  def update
    authorize! :update, @user
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to user_index_path }
      else
        format.html { render :index }
      end
    end
  end

  private

  def set_user
    @user = User.find(params[:id])
  end

  def user_params
    params.permit(:active)
  end

上述控制器的规范代码

RSpec.describe UserController, type: :controller do
    describe 'GET #index' do
      let(:user) {User.create!(name: "hari")}
      context 'with user details'do
        it 'loads correct user details' do
          get :index

      expect(response).to permit(:user)
    end
      end

      context 'without user details' do
        it 'doesnot loads correct user details' do
          get :index

        expect(response).not_to permit(:user)
      end
    end
    end

    describe 'Patch #update' do

      context 'when valid params' do
        let(:attr) do
          {active: 'true'}
        end

        before(:each) do
       @user = subject.current_user
       put :update, params: { user: attr }
       @user.reload
     end

     it 'redirects to user_index_path ' do
       expect(response).redirect_to(user_index_path)
     end

     it 'sets active state' do
       expect(@user.active?('true')).to be true
     end
   end

   context 'when invalid param' do
     let(:attr) do
       {active: 'nil'}
     end

     before(:each) do
       @user = subject.current_user
       put :update, params: { user: attr }
       @user.reload
     end

     it 'render index' do
       expect(respone.status).to eq(200)
     end

     it 'doesnot change active state' do
       expect(@user.active?(nil)).to be true
     end
   end
  end
 end

我只是一个初学者,通过检查https://relishapp.com/rspec/rspec-rails/docs/gettingstarted来尝试控制器的规范代码。你可以帮我解决我的规格出错的问题,或者有人能给我一些这些方法的测试例子,还是可以将我重定向到rspec指南?索引方法失败了 和我的

终端日志

1) UserController GET #index with user details loads correct user details
     Failure/Error: expect(response).to permit(:user)

     NoMethodError:
       undefined method `permit' for #<RSpec::ExampleGroups::UserController::GETIndex::WithUserDetails:0x00005614152406b0>
       Did you mean?  print
     # ./spec/controllers/user_controller_spec.rb:10:in `block (4 levels) in <top (required)>'

0 个答案:

没有答案