我正在使用Rails 5.001,我想从shared_list测试一个控制器函数。 Shared_list嵌套在shopping_list下。
shared_lists_controller_test.rb
class SharedListsControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
include Warden::Test::Helpers
setup do
new_shopping_list = shopping_lists(:shopping_list_drogerie)
@heikoSharedList = shared_lists(:shared_list_heiko)
@heiko = users(:user_heiko)
end
test "should get edit" do
login_as(@heiko)
@heiko.confirmed_at = Time.now
get edit_shared_list_url(@heikoSharedList.shopping_list.id, @heikoSharedList)
assert_response :success
end
但是,当我运行测试时,收到此错误消息:
Error:
SharedListsControllerTest#test_should_get_edit:
ActiveRecord::RecordNotFound: Couldn't find SharedList with 'id'=102138810
test/controllers/shared_lists_controller_test.rb:49:in `block in <class:SharedListsControllerTest>'
有人知道出了什么问题以及如何解决这个问题? 我的装置看起来像这样:
shared_lists
shared_list_heiko:
user: user_heiko
shopping_list: shopping_list_drogerie
created_at: <%= Time.now %>
updated_at: <%= Time.now %>
shopping_lists
shopping_list_drogerie:
user: user_heiko
name: Drogerie
created_at: <%= Time.now %>
updated_at: <%= Time.now %>
模型/ list_item.rb
class ListItem < ApplicationRecord
# db associations
belongs_to :shopping_list
has_many :shopping_items
# validations
validates :shopping_list, :presence => true
validates :name, presence: true, allow_blank: false
end
模型/ shopping_list.rb
class ShoppingList < ApplicationRecord
# db associations
belongs_to :user
# if a shopping list is deleted, also delete information about all items on the list
has_many :list_items, :dependent => :destroy
# if a shopping list is deleted, also delete information about who it was shared with
has_many :shared_lists , :dependent => :destroy
has_many :shared_with_users,through: :shared_lists, :source => :user
has_many :invitation
has_one :appointment
# validations
validates :user, :presence => true
validates :name, presence: true, allow_blank: false, uniqueness: {scope: :user_id}
end
控制器/ shopping_lists_controller.rb
class ShoppingListsController < ApplicationController
load_and_authorize_resource
# GET /shopping_lists/1
# GET /shopping_lists/1.json
def show
end
# POST /shopping_lists
# POST /shopping_lists.json
def create
respond_to do |format|
if @shopping_list.save
format.html { redirect_to shopping_list_list_items_path(@shopping_list), alert: 'Shopping list was successfully created.' }
format.json { render :show, status: :created, location: @shopping_list }
else
format.html { render :new }
format.json { render json: @shopping_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /shopping_lists/1
# PATCH/PUT /shopping_lists/1.json
def update
respond_to do |format|
if @shopping_list.update(shopping_list_params)
format.html { redirect_to @shopping_list, notice: 'Shopping list was successfully updated.' }
format.json { render :show, status: :ok, location: @shopping_list }
else
format.html { render :edit }
format.json { render json: @shopping_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /shopping_lists/1
# DELETE /shopping_lists/1.json
def destroy
@shopping_list.destroy
respond_to do |format|
format.html { redirect_to shopping_lists_url, notice: 'Shopping list was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_shopping_list
@shopping_list = ShoppingList.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
private def shopping_list_params
params.require(:shopping_list).permit(:name)
end
end