Ruby on Rails:对嵌套表单执行功能测试

时间:2012-12-10 15:12:03

标签: ruby-on-rails nested-attributes functional-testing

我有一个包含3个模型的rails应用程序:image, profile and user do模型之间的关系如下: 用户has_one个人资料 用户has_many图像 图片和个人资料属于用户

我有脚手架为我的初始用户模型创建的功能测试,但现在其中一些不再起作用了。 我想编写一个测试,它将覆盖整个表单,包含嵌套属性等。 现在,所有数据库表都由单个页面上的嵌套表单填充,并且我的一些功能测试失败并出现错误。 以下是相关测试和错误:

  test "should create user" do
    assert_difference('User.count') do
      post :create, :user => { :email => "testing@gmail.com",
                :first_name =>  "langlylo",
                :last_name => "aaaaa", 
                :orientation => "Straight",
                :password => "secret",
                :password_confirmation => "secret", 
                :gender => "Male",
                :location => "London" }
    end

  test "should show user" do
    get :show, id: @user
    assert_response :success
  end

  test "should update user" do
    # @user = user.authenticate 
    put :update, :id => @user.to_param, :user => @user
    assert_redirected_to @user
  end

 1) Failure:
test_should_create_user(UsersControllerTest) [/home/tim/fairym/test/functional/users_controller_test.rb:20]:
"User.count" didn't change by 1.
<4> expected but was
<3>.

  2) Error:
test_should_show_user(UsersControllerTest):
ActionView::Template::Error: undefined method `height' for nil:NilClass
    /home/tim/fairym/app/views/users/show.html.erb:30:in `_app_views_users_show_html_erb___2480425410967695566_34986560'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/template.rb:145:in `block in render'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications.rb:123:in `block in instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications.rb:123:in `instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/template.rb:143:in `render'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications.rb:123:in `block in instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications.rb:123:in `instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/renderer/template_renderer.rb:45:in `render_template'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/renderer/template_renderer.rb:18:in `render'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/renderer/renderer.rb:36:in `render_template'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_view/renderer/renderer.rb:17:in `render'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/rendering.rb:110:in `_render_template'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/streaming.rb:225:in `_render_template'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/rendering.rb:103:in `render_to_body'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/renderers.rb:28:in `render_to_body'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/rendering.rb:88:in `render'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/rendering.rb:16:in `render'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/core_ext/benchmark.rb:5:in `block in ms'

  3) Error:
test_should_update_user(UsersControllerTest):
NoMethodError: undefined method `stringify_keys' for "980190962":String
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/attribute_assignment.rb:69:in `assign_attributes'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/persistence.rb:215:in `block in update_attributes'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/transactions.rb:208:in `transaction'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/persistence.rb:214:in `update_attributes'
    /home/tim/fairym/app/controllers/users_controller.rb:79:in `block in update'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/mime_responds.rb:270:in `call'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/mime_responds.rb:270:in `retrieve_collector_from_mimes'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/mime_responds.rb:194:in `respond_to'
    /home/tim/fairym/app/controllers/users_controller.rb:78:in `update'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/base.rb:167:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/rendering.rb:10:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:414:in `_run__4169349413879532592__process_action__1508431050785970058__callbacks'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/callbacks.rb:17:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/rescue.rb:29:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications.rb:123:in `block in instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications.rb:123:in `instrument'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/base.rb:121:in `process'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/rendering.rb:45:in `process'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:469:in `process'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:49:in `process'
    /home/tim/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:396:in `put'
    /home/tim/fairym/test/functional/users_controller_test.rb:46:in `block in <class:UsersControllerTest>'

8 tests, 8 assertions, 1 failures, 2 errors, 0 skips

新用户视图:

<p><h1>com</h1></p>
<p><h2>Signup Page</h2></p>
<%= form_for @user do |f| %>
    <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% for message in @user.errors.full_messages %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
        <%= f.label :gender, "I am a:" %><%= f.select :gender, options_for_select([["Man", "Male"], ["Woman", "Female"]]) %><br />
        <%= f.label :orientation, "Sexsual Orientation" %><%= f.select :orientation, options_for_select([["Straight", "Straight"], ["Gay", "Gay"], ["Bi","Bi"]]) %><br />
        <%= f.label :first_Name %><br /><%= f.text_field :first_name %><br />
        <%= f.label :last_name %><br /><%= f.text_field :last_name %><br />
        Date of Birth:<%= f.date_select( :date_of_birth, :start_year => 1920, :prompt => { :day => 'day', :month => 'month', :year => 'year' }) %><br />
        <%= f.label :location %><br /><%= f.text_field :location %><br />
        <%= f.label :email %><br /><%= f.text_field :email %><br />
        <%= f.label :password %><br /><%= f.password_field :password %><br />
            <%= f.label :password_confirmation %><br /><%= f.password_field :password_confirmation %><br />

        <%= f.fields_for :profile do |builder| %>
          <fieldset>
            <%= builder.label :height, "My height is: (cm)" %><%= builder.select :height, options_for_select(140..220) %><br />
            <%= builder.label :weight, "My weight is: (kg)" %><%= builder.text_field :weight %><br />
            <%= builder.label :body_type, "Body Type:" %><%= builder.select :body_type, options_for_select([["Curvy", "Curvy"], ["Fat", "Fat"], ["Slim","Slim"],["Average","Average"],["Athletic","Athletic"]]) %><br />
            <%= builder.label :ethnicity, "Ethnicity:" %><%= builder.select :ethnicity, options_for_select([["White", "White"], ["Black", "Black"], ["Blue","Blue"],["Asian","Asian"],["Other","Other"]]) %><br />
            <%= builder.label :religion, "Religion:" %><%= builder.select :religion, options_for_select([["Christian", "Christian"], ["Muslim", "Muslim"], ["Atheist","Atheist"],["Jewish","Jewish"],["Hindu","Hindu"]]) %><br />
            <%= builder.label :drinking, "Drinking:" %><%= builder.select :drinking, options_for_select([["Every day", "Every day"], ["A few times a week", "A few times a week"], ["Once a week","Once a week"],["Rarely","Rarely"],["Never","Never"]]) %><br />
            <%= builder.label :smoking, "Smoking:" %><%= builder.select :smoking, options_for_select([["Heavily", "Heavily"], ["Regularly", "Regularly"], ["When drinking","When drinking"],["Sometimes","Sometimes"],["Never","Never"]]) %><br />
            <%= builder.label :looking_for, "Looking For:" %><%= builder.text_field :looking_for %><br />
            <%= builder.label :physically_active, "Physically Active:" %><%= builder.select :physically_active, options_for_select([["All the time", "All the time"], ["Sometimes", "Sometimes"], ["When drinking","When drinking"], ["Rarely","Rarely"]]) %><br />
            <%= builder.label :cultural_favorites, "Favorite Music, Movies, Books:" %><%= builder.text_area :cultural_favorites, :rows => 5%><br />
          </fieldset>
        <% end %>
<%= f.fields_for :images do |z| %>
<fieldset>
<p>
    <%= z.label :image_title %><br />
    <%= z.text_field :image_title %>
    <%= z.hidden_field :image_priority, :value => '1' %> 
  </p>
  <p>
    <%= z.file_field :image_location %>
  </p>
</fieldset>
<% end %>

</div>
</script>
    <%= f.submit "Next" %>
<% end %>

<%= link_to 'Back', users_path %>

浏览视图:

<h1>Find a match</h1>

<table border="0">


    <%= render 'userbox' %>

</table>


<br />

<%= link_to 'New User', new_user_path %> <%= link_to 'Logout', logout_path %>

userbox partial:

<% @users.each do |user| %>     
<tr>

    <td>
          <% user.images.each do |img| %>
                <%= image_tag img.image_location_url.to_s %>
            <% end %>            
        </td>
    <td valign="top">
    <p>
        <%= link_to user.first_name, user_path(user) %></p>
    <p> <%= age(user.date_of_birth) %> / <%= user.gender %> / <%= user.orientation %></p>
    <p> from <%= user.location %></p>
    <p> Question? Answer answer answer answer answer answer answer</p>
</td>
  </tr>
  <% end %>

用户控制器:

class UsersController < ApplicationController
  # GET /users
  # GET /users.json
  def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  end

  def browse
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  end

  # GET /users/1
  # GET /users/1.json
  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @user }
    end
  end

  # GET /users/new
  # GET /users/new.json
  def new       
    @user = User.new
    @user.build_profile
    @user.images.build


    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
  end

  # GET /users/1/edit
  def edit
    @user = User.find(params[:id])
  end

  # POST /users
  # POST /users.json
  def create   
    @user = User.new(params[:user])
    @user.date_joined = DateTime.now.to_date
    @user.last_online = DateTime.now.to_date
    @user.admin = false


  respond_to do |format|
      if @user.save
        format.html { redirect_to browse_path, notice: 'User was successfully created.' }
        format.json { render json: @user, status: :created, location: @user }
    format.js
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
  end
end

  # PUT /users/1
  # PUT /users/1.json
  def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
  end
end

0 个答案:

没有答案