Rspec怎么写我的测试?

时间:2016-10-10 14:09:43

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

在我可以创建教程的应用程序中,我开始测试它,我很难创建正确的测试。

这是我在 spec / features / tutos / create_spec.rb

中的内容

我该怎么写def create_tuto ??

require "spec_helper"
require "rails_helper"

context "Creating a tuto" do
  let!(:user){ User.create(first_name: "John", last_name: "Doe", email: "john@email.com", password:"password", password_confirmation:"password")
}

  def create_tuto(options={})
    options[:title]    ||= "My tuto"
    options[:category] ||= "Ruby"
    options[:content]  ||= "This contains markdown language"

    visit "en/tutos"
    click_link "create_tuto"
    expect(page).to have_content("Create your tutorial")

    fill_in "Title",  with: options[:title]
    #I don't know here how to do the dropdown select ?
    select("Ruby", from: "Chose a category")
    fill_in "Content", with: options[:content]
    click_button "Save"
  end

  it "redirect to the tuto list show page on success" do
    create_tuto
    expect(page).to have_content("Tuto was successfully created")
  end

end

当我启动测试时,我总是遇到问题

 Capybara::ElementNotFound:
       Unable to find link "create"

我的 create tuto 位于我的部分搜索表单的底部:  的视图/ TUTOS / _search_form.html.slim

h1.text-gray =t("search_form.title") 
.search_banner
  .row
    .col-xs-12.col-sm-4
      =form_tag tutos_path, :method => 'get' do 
        =text_field_tag :search, params[:search],  placeholder: t("search_form.keyword"), class: 'search-form'
        =submit_tag t("search_form.search"), class:'btn btn-xs btn-default btn-search'
        br
        br

    .col-xs-12.col-sm-4
      =form_tag tutos_path, :method => 'get' do 
        =select_tag :select,
             options_for_select(User.order('nickname ASC').all.map{|u| u.nickname}, params[:select]),
             { include_blank: t("search_form.select_user"), class: 'search-form' }

        =submit_tag t("search_form.select"), class:"btn btn-xs btn-default btn-search"
        br
        br

    .col-xs-12.col-sm-4
      =form_tag tutos_path, :method => 'get' do 
        =select_tag :filter, 
              options_for_select(Category.all.map{|c| c.name}, params[:filter]),
              { include_blank: t("search_form.select_cat"),  class: 'search-form' }

        =submit_tag t("search_form.search"), class:"btn btn-xs btn-default btn-search"
        br
        br

  hr  
  .row
    .col-xs-12.col-sm-3
      = link_to t("search_form.best"),  best_voted_path, class: "btn btn-default" 

    .col-xs-12.col-sm-3
      -if user_signed_in?
        = link_to t("search_form.create"), new_tuto_path, class:"btn btn-default", id:"create_tuto"

视图/ TUTOS / _tutos_form.html.slim 这部分是为了创建一个新的教程

= simple_form_for @tuto do |f|
  - if @tuto.errors.any?
    #error_explanation
      h2 = "#{pluralize(@tuto.errors.count, "error")} prohibited this tuto from being saved:"
      ul
        - @tuto.errors.full_messages.each do |message|
          li = message
  = f.hidden_field :user_id, value: current_user.id
  = f.input :title, label: t('tutos_form.title')
  = f.collection_select :category_id, Category.all, :id, :name, { include_blank: t("tutos_form.category"), class: 'search-form' }
  = f.input :content, label: t('tutos_form.content'), as: :text, input_html: { rows: "15" }
  = f.button :submit, t("tutos_form.save")

修改

我在create_tuto方法中更改了一件事: 而不是click_link "create_tuto"我放了click_button I18n.t("search_form.create")

  def create_tuto(options={})
    options[:title]    ||= "My tuto"
    options[:category] ||= "Ruby"
    options[:content]  ||= "This contains markdown language"

    visit "/en/tutos/"
    click_button I18n.t("search_form.create")
    expect(page).to have_content("Create your tutorial")

    fill_in "Title",  with: options[:title]
    select("Ruby", from: "Chose a category")
    fill_in "Content", with: options[:content]
    click_button "Save"
  end

现在我有这个失败:

Failures:

  1) Creating a tuto redirect to the tuto list show page on success
     Failure/Error: click_button I18n.t("search_form.create")

     Capybara::ElementNotFound:
       Unable to find button "Create a tuto"
     # /Users/nelly/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/finders.rb:44:in `block in find'
     # /Users/nelly/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/base.rb:85:in `synchronize'
     # /Users/nelly/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/finders.rb:33:in `find'
     # /Users/nelly/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/actions.rb:57:in `click_button'
     # /Users/nelly/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/session.rb:735:in `block (2 levels) in <class:Session>'
     # /Users/nelly/.rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
     # ./spec/features/tutos/create_spec.rb:14:in `create_tuto'
     # ./spec/features/tutos/create_spec.rb:24:in `block (2 levels) in <top (required)>'

也许这可能会有所帮助!

按钮&#34;创建Tuto&#34;要求显示签名用户

我怎样才能编写测试?

0 个答案:

没有答案