-1
我是RSpec的新手。這些規格是由腳手架產生的。爲什麼這些RSpec示例失敗?
我在我的日誌面臨這些錯誤訊息:
Failures:
1) ClientsController POST create with invalid params assigns a newly created but unsaved client as @client
Failure/Error: expect(assigns(:client)).to be_a_new(Client)
expected #<Client id: 1, name: "", phone: "", email: "", status: nil, created_at: "2016-05-14 14:56:01", updated_at: "2016-05-14 14:56:01"> to be a new Client(id: integer, name: string, phone: string, email: string, status: integer, created_at: datetime, updated_at: datetime)
# ./spec/controllers/clients_controller_spec.rb:88:in `block (4 levels) in <top (required)>'
2) ClientsController POST create with invalid params re-renders the 'new' template
Failure/Error: expect(response).to render_template("new")
expecting <"new"> but rendering with <[]>
# ./spec/controllers/clients_controller_spec.rb:93:in `block (4 levels) in <top (required)>'
3) ClientsController PUT update with invalid params re-renders the 'edit' template
Failure/Error: expect(response).to render_template("edit")
expecting <"edit"> but rendering with <[]>
# ./spec/controllers/clients_controller_spec.rb:139:in `block (4 levels) in <top (required)>'
Finished in 0.28667 seconds (files took 5.4 seconds to load)
16 examples, 3 failures
我的規格低於:
require 'rails_helper'
RSpec.describe ClientsController, :type => :controller do
let(:valid_attributes) {
{
"name" => "Mark Rodwell",
"phone" => '5585985463053',
"email" => '[email protected]',
"status" => 1
}
}
let(:invalid_attributes) {
{
"name" => "",
"phone" => '',
"email" => '',
"status" => nil
}
}
let(:valid_session) {
{
"user_type" => 2,
"status" => 1,
"name" => "Cristiano Carvalho",
"email" => '[email protected]',
"password" => '12345678',
"password_confirmation" => '12345678'
}
}
describe "GET index" do
it "assigns all clients as @clients" do
client = Client.create! valid_attributes
get :index, {}, valid_session
expect(assigns(:clients)).to eq([client])
end
end
describe "GET show" do
it "assigns the requested client as @client" do
client = Client.create! valid_attributes
get :show, {:id => client.to_param}, valid_session
expect(assigns(:client)).to eq(client)
end
end
describe "GET new" do
it "assigns a new client as @client" do
get :new, {}, valid_session
expect(assigns(:client)).to be_a_new(Client)
end
end
describe "GET edit" do
it "assigns the requested client as @client" do
client = Client.create! valid_attributes
get :edit, {:id => client.to_param}, valid_session
expect(assigns(:client)).to eq(client)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new Client" do
expect {
post :create, {:client => valid_attributes}, valid_session
}.to change(Client, :count).by(1)
end
it "assigns a newly created client as @client" do
post :create, {:client => valid_attributes}, valid_session
expect(assigns(:client)).to be_a(Client)
expect(assigns(:client)).to be_persisted
end
it "redirects to the created client" do
post :create, {:client => valid_attributes}, valid_session
expect(response).to redirect_to(Client.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved client as @client" do
post :create, {:client => invalid_attributes}, valid_session
expect(assigns(:client)).to be_a_new(Client)
end
it "re-renders the 'new' template" do
post :create, {:client => invalid_attributes}, valid_session
expect(response).to render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
let(:new_attributes) {
{
"name" => "Mark James Rodwell",
"phone" => '5585985463053',
"email" => '[email protected]',
"status" => 1
}
}
it "updates the requested client" do
client = Client.create! valid_attributes
put :update, {:id => client.to_param, :client => new_attributes}, valid_session
client.reload
expect(assigns(:client)).to eq(client)
end
it "assigns the requested client as @client" do
client = Client.create! valid_attributes
put :update, {:id => client.to_param, :client => valid_attributes}, valid_session
expect(assigns(:client)).to eq(client)
end
it "redirects to the client" do
client = Client.create! valid_attributes
put :update, {:id => client.to_param, :client => valid_attributes}, valid_session
expect(response).to redirect_to(client)
end
end
describe "with invalid params" do
it "assigns the client as @client" do
client = Client.create! valid_attributes
put :update, {:id => client.to_param, :client => invalid_attributes}, valid_session
expect(assigns(:client)).to eq(client)
end
it "re-renders the 'edit' template" do
client = Client.create! valid_attributes
put :update, {:id => client.to_param, :client => invalid_attributes}, valid_session
expect(response).to render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested client" do
client = Client.create! valid_attributes
expect {
delete :destroy, {:id => client.to_param}, valid_session
}.to change(Client, :count).by(-1)
end
it "redirects to the clients list" do
client = Client.create! valid_attributes
delete :destroy, {:id => client.to_param}, valid_session
expect(response).to redirect_to(clients_url)
end
end
end
如果您發佈展示該問題的最小代碼示例,它確實有幫助。例如,當你的問題是關於3個失敗的問題時,不需要發佈13個規範,當你的問題是關於3個失敗的問題時:80%的發佈代碼只是噪聲讀者必須滾動 –
@Cristiano Carvalho你的解答是什麼? –