如何格式化以下行以向/users/1
发送请求?
get :show, {format: :json, id: @user1.id }
我尝试过每一种排列和支架安排
结果错误:
NoMethodError:
undefined method `json' for #<UsersController
整个规范文件:
描述UsersController,:type =&gt; :控制器做 要求&#39; database_cleaner&#39;
DatabaseCleaner.strategy = :truncation
describe "GET users" do
before(:all) do
@user1 = FactoryGirl.create :user, name: "Yakov", default_pull_rate: 10, default_push_rate: 9
@user2 = FactoryGirl.create :user, name: "Smirnov", default_pull_rate: 15, default_push_rate: 1
@user1.save
@user2.save
@player1 = FactoryGirl.create :player, user_id: 1
@player1.save
end
it "returns all users" do
get :index, :format => :json
expect(response.status).to eq 200
body = JSON.parse(response.body)
user_names = body.map { |u| u["name"] }
expect(user_names).to match_array(["Yakov", "Smirnov"])
end
it "returns player for a user" do
get :show, {format: :json, id: @user1.id }
expect(response.status).to eq 200
end
DatabaseCleaner.clean
end
端
控制器:
class UsersController < ApplicationController
include ActionController::MimeResponds
def index
@users = User.all
respond_to do |format|
format.json { render json: @users }
end
end
def show
@user = User.find(params[:id])
respond_to do |format|
format.json { render json @user }
end
end
def create
@user = PowerUp.new(params[:user])
@user.save!
end
end
答案 0 :(得分:1)
您的测试工作正常。它实际上向您显示控制器中的问题。
在show
行动中,您有
format.json { render json @user }
应该是
format.json { render json: @user }