我在做什麼錯?失敗/錯誤:expect(迴應).to render_template(:new)
Failures:
1) AccountsController POST :create an account that already exists renders the :new template Failure/Error: expect(response).to render_template(:new) expecting <"new"> but rendering with <[]> # ./spec/controllers/accounts_controller_spec.rb:131:in `block (4 levels) in '
Finished in 0.26832 seconds 34 examples, 1 failure
Failed examples:
rspec ./spec/controllers/accounts_controller_spec.rb:128 # AccountsController POST :create an account that already exists renders the :new template
驗證我的代碼是由在軌運行控制檯它正確地寫道:當我rspec的運行我不斷收到此錯誤。而且足夠真實,它呈現:new模板,甚至返回200.我通過在rails控制檯中使用應用程序對象來做到這一點。輸出如下:
Started POST "/accounts" for 127.0.0.1 at 2014-03-26 21:18:06 -0700
Processing by AccountsController#create as HTML
Parameters: {"account"=>{"name"=>"name", "description"=>"desc", "opening_balance"=>"1.0"}}
(0.1ms) begin transaction
Account Exists (0.2ms) SELECT 1 AS one FROM "accounts" WHERE "accounts"."name" = 'name' LIMIT 1
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
(0.1ms) rollback transaction
**Rendered accounts/new.html.erb within layouts/application (1.4ms)**
Completed 200 OK in 44ms (Views: 32.8ms | ActiveRecord: 0.3ms)
=> 200
這會創建兩次相同的帳戶。我預計,它會失敗,然後重定向到:新模板。 rspec代碼在這裏:
context 'an account that already exists' do
it 'renders the :new template' do
call_post_create_verb
call_post_create_verb
**expect(response).to render_template(:new)**
end
end
看看else條件,你會發現我渲染的地方:new。控制器代碼是在這裏:
def ceate
@account = Account.new(account_params)
if @account.save
redirect_to @account
else
**render :new**
end
end
這是我的設置: 紅寶石2.1.1p76(2014年2月24日修訂45161)x86_64的-darwin13.0] 軌(4.0.3) RSpec的護欄(2.14.1)
你真的打電話創建兩次在你的規範? –
我們需要看到更多的上下文,比如規範的實際內容(什麼是「call_post_create_verb」?) – sevenseacat
清除測試日誌並運行失敗的規範。檢查測試日誌,因爲它可能會指出一些新的信息。 –