브라우저에서 테스트하여 올바르게 작동합니다. 테스트 오류는 "예상 결과가 0에서 1로 변경되었지만 변경되지 않았습니다"라고 표시됩니다. 이것은 공장 문제입니까 rspec 문제입니까? 왜 변하지 않는거야?Rspec에서 컨트롤러를 사용하여 테스트 하시겠습니까?
오류 :
Failures:
1) ShortLinksController Short links controller Clicking a short link increments the click counter by 1
Failure/Error: expect{ get :url_dispatch, { id: short_link.short_link } }.to change{short_link.click_counter}.from(0).to(1)
expected result to have changed from 0 to 1, but did not change
# ./spec/controllers/short_links_controller_spec.rb:34:in `block (4 levels) in <top (required)>'
RSpec에 :
it "increments the click counter by 1" do
short_link = create(:short_link)
expect{ get :url_dispatch, { id: short_link.short_link } }.to change{short_link.click_counter}.from(0).to(1)
end
컨트롤러 :
def url_dispatch
id = params[:id]
record = ShortLink.where(["short_link = ?", id]).first
if record.update(click_counter: record.click_counter + 1)
redirect_to record.redirect_to
else
render '/not_found'
end
end
공장 :
FactoryGirl.define do
factory :short_link do
redirect_to "http://google.com"
title "This is the google page"
short_link "xGh7u"
click_counter 0
owner Owner.create!(first_name: "Bob", last_name: "Diller", email: "[email protected]")
end
end
팩토리 대신 모델을 호출하면 동일한 코드가 제대로 작동한다는 사실을 기반으로 한 'FactoryGirl'문제라고 생각합니다. 나는 컨트롤러 테스트에서이 문제를 해결하기 위해'reload'를 사용하고 있습니다.하지만 누군가 대답을하면 궁금합니다. – Anthony
안녕하세요 @Anthony thats interesting. 나는 그것도 FG 문제라고 생각한다. 대답으로 작품을 게시 할 수 있습니까? 감사. – fyz