rspec 参数测试失败

发布于 2025-01-12 13:26:00 字数 1498 浏览 2 评论 0原文

我是 Rails 新手,我正在尝试为控制器方法创建编写规范测试,但我不知道如何传递参数用户来获取方法。 这是项目控制器

  def create
      @project = Project.new(project_params)
      @project.creator = current_user
      byebug
      if params[:project][:users].any?
        byebug
        params[:project][:users].reject!(&:empty?)
        @project.enrolled_user = User.find( params[:project][:users])
      end
    byebug
      authorize @project
      respond_to do |format|
        if @project.save
          format.html { redirect_to @project, notice: 'Project was successfully 
           created.'}
          format.json { render :show, status: :created, location: @project}
        else
          format.html { render :new}
          format.json { render json: @project.errors, status: :unprocessable_entity}
        end
      end
    end

project_spec.rb

  context "post create" do
  it "creates a new project" do
    get :new
    byebug
   
    post :create,params: { user: user.id }
    byebug
    

    expect(project1.creator).to eq(user)
    expect(enrolled_user.user_id).to eq(user1.id)
    expect(response).to be_successful
  end
end

终端的创建方法

 1) ProjectsController post create creates a new project
 Failure/Error: if params[:project][:users].any?
 
 NoMethodError:
   undefined method `[]' for nil:NilClass
 # ./app/controllers/projects_controller.rb:38:in `create'
 # ./spec/requests/project_spec.rb:49:in `block (3 levels) in <main>'

i'm new to rails , i'm tring to write spec test for controller method create and I can't figure out how to pass params users to get method.
here is create method for project controller

  def create
      @project = Project.new(project_params)
      @project.creator = current_user
      byebug
      if params[:project][:users].any?
        byebug
        params[:project][:users].reject!(&:empty?)
        @project.enrolled_user = User.find( params[:project][:users])
      end
    byebug
      authorize @project
      respond_to do |format|
        if @project.save
          format.html { redirect_to @project, notice: 'Project was successfully 
           created.'}
          format.json { render :show, status: :created, location: @project}
        else
          format.html { render :new}
          format.json { render json: @project.errors, status: :unprocessable_entity}
        end
      end
    end

project_spec.rb

  context "post create" do
  it "creates a new project" do
    get :new
    byebug
   
    post :create,params: { user: user.id }
    byebug
    

    expect(project1.creator).to eq(user)
    expect(enrolled_user.user_id).to eq(user1.id)
    expect(response).to be_successful
  end
end

terminal

 1) ProjectsController post create creates a new project
 Failure/Error: if params[:project][:users].any?
 
 NoMethodError:
   undefined method `[]' for nil:NilClass
 # ./app/controllers/projects_controller.rb:38:in `create'
 # ./spec/requests/project_spec.rb:49:in `block (3 levels) in <main>'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2025-01-19 13:26:00

您确实需要系统测试而不是控制器测试来满足测试此处描述的功能的要求。所以 Rspec 与 Capybara 结合将允许你做这样的事情

所以使用rails g rspec:system project
并进行诸如有关

  it "Can create a project" do
    administrator_sign_in #some system test helper that creates a user and logs a user in if needed, there are plenty of examples on how to do helpers like this.
    visit '/admin/projects'
    click_link "New" 
    #save_and_open_page #Uncomment this if you want to see what your form fields are, unstyled, can be very useful when trying to see what to fill in and what button and links to press and click
    fill_in "Name", with: "New project"
    fill_in "Other project fields", with: "Some Data"
    page.check("Name of a boolean field") #check a checkbox
    click_button("Create Project")
    project = Project.find_by name: "New project"
    expect(current_path).to eq("/admin/projects/#{project.id}")
    expect(page).to have_content("Project was successfully created")
    expect(project.name_of_boolean_field).to be true
  end

生成器的更多信息此处

有关系统测试的更多信息此处

有关水豚的更多信息此处

You really need a system test rather than a controller test to achieve your requirement to test the functionality you describe here. So Rspec coupled with Capybara will allow you to do things like this

So use rails g rspec:system project
and do tests like

  it "Can create a project" do
    administrator_sign_in #some system test helper that creates a user and logs a user in if needed, there are plenty of examples on how to do helpers like this.
    visit '/admin/projects'
    click_link "New" 
    #save_and_open_page #Uncomment this if you want to see what your form fields are, unstyled, can be very useful when trying to see what to fill in and what button and links to press and click
    fill_in "Name", with: "New project"
    fill_in "Other project fields", with: "Some Data"
    page.check("Name of a boolean field") #check a checkbox
    click_button("Create Project")
    project = Project.find_by name: "New project"
    expect(current_path).to eq("/admin/projects/#{project.id}")
    expect(page).to have_content("Project was successfully created")
    expect(project.name_of_boolean_field).to be true
  end

More info on generators here

More info on system tests here

More info on Capybara here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文