courses#new 填寫表單
編輯 spec/controllers/courses_controller_spec.rb
,加入另一段 Test。
你可以注意到:new
這段測試裡面,好像一次測了兩個東西?
這豈不是違反了 One assertion per test 的原則嗎?
其實這樣的寫法不能算是破壞規則,因為其實這兩行是在測一件事 @course = Course.new
,只是在 RSpec 裡限於語法的關係只能拆開寫...
describe "GET new" do
it "assign @course" do
course = build(:course)
get :new
expect(assigns(:course)).to be_a_new(Course)
end
it "render template" do
course = build(:course)
get :new
expect(response).to render_template("new")
end
end
be_a_new
是 RSpec 針對 Controller Spec 提供的方法:https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs。
$ rspec spec/controllers/courses_controller_spec.rb
....FF
Failures:
1) CoursesController GET new assign @course
Failure/Error: get :new
AbstractController::ActionNotFound:
The action 'new' could not be found for CoursesController
2) CoursesController GET new render template
Failure/Error: get :new
AbstractController::ActionNotFound:
The action 'new' could not be found for CoursesController
Finished in 0.07564 seconds (files took 1.18 seconds to load)
6 examples, 2 failures
Failed examples:
rspec ./spec/controllers/courses_controller_spec.rb:43 # CoursesController GET new assign @course
rspec ./spec/controllers/courses_controller_spec.rb:51 # CoursesController GET new render template
new
action 沒有實現。
補上 action
在 app/controllers/courses_controller.rb
加入 new
這個 action
def new
@course = Course.new
end
$ rspec spec/controllers/courses_controller_spec.rb
....FF
Failures:
1) CoursesController GET new assign @course
Failure/Error: get :new
ActionController::UnknownFormat:
CoursesController#new is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not… nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
2) CoursesController GET new render template
Failure/Error: get :new
ActionController::UnknownFormat:
CoursesController#new is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not… nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
Finished in 0.08812 seconds (files took 1.18 seconds to load)
6 examples, 2 failures
Failed examples:
rspec ./spec/controllers/courses_controller_spec.rb:43 # CoursesController GET new assign @course
rspec ./spec/controllers/courses_controller_spec.rb:51 # CoursesController GET new render template
補上 view
touch app/views/courses/new.html.erb
加入
<div class="col-md-12">
<%= form_for @course do |f| %>
<%= f.text_field :title %>
<%= f.text_area :description %>
<div class="form-actions">
<%= f.submit "Submit", :disable_with => 'Submiting...', :class => "btn btn-primary"%>
</div>
<% end %>
</div>
$ rspec spec/controllers/courses_controller_spec.rb
......
Finished in 0.05946 seconds (files took 1.14 seconds to load)
6 examples, 0 failures