这是我的控制器测试:
test "should get create" do
sign_in(FactoryGirl.create(:user, admin: true))
assert_difference('Event.count') do
post :create, FactoryGirl.build(:event)
end
assert_not_nil assigns(:event)
assert_response :success
end
当我向 events.rb 添加最简单的验证时
class Event < ActiveRecord::Base
attr_accessible :city, :date, :name, :state, :street
has_many :periods
validates :name, presence: true
end
我得到:
1) Failure:
test_should_get_create(EventsControllerTest) [/Users/noahc/Dropbox/mavens/test/functional/events_controller_test.rb:37]:
"Event.count" didn't change by 1.
<2> expected but was
<1>.
但是,然后我查看 events_factory.rb
factory :event do
name 'First Event'
street '123 street'
city 'Chicago'
state 'IL'
date Date.today
end
并且似乎没有需要名称的问题。
更新:
当我进行测试时:
test "should get create" do
sign_in(FactoryGirl.create(:user, admin: true))
assert_not_nil assigns(:event)
assert_response :success
end
我得到:
1) Failure:
test_should_get_create(EventsControllerTest) [/Users/noahc/Dropbox/mavens/test/functional/events_controller_test.rb:38]:
<nil> expected to not be nil.
当我删除该行并留在 assert_response :success 中时,它通过了。
更新2:
def create
@event = Event.new(params[:event])
@event.save
end