David M
2012-Mar-14 22:38 UTC
How do I create a new object in testing to test a controller?
I''m using Test::Unit to do functional testing on one of my controllers.
The action I want to test is create, from SessionsController. I have
simplified it to the extreme:
def create
@user = User.find_by_email(params[:email])
if @user
flash.now[:notice] = "ok, it exists"
else
flash.now[:notice] = "it does not exist"
end
render :new
end
This is my functional test:
require ''test_helper''
class SessionsControllerTest < ActionController::TestCase
test "object creation" do
user = User.create!(email:
"user-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org", password:
"secret")
assert user.valid? # make sure User.new is correct
assert user.persisted? # make sure it''s persisted
post :create, post: { email:
"user-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org" }
assert_equal "ok, it exists", flash.now[:notice]
end
end
And this is the execution flow:
> $ bundle exec rake db:test:prepare
$ ruby -Itest test/functional/sessions_controller_test.rb -n
test_object_creation
Run options: -n test_object_creation
# Running tests:
F
Finished tests in 0.235903s, 4.2390 tests/s, 12.7171 assertions/s.
1) Failure:
test_object_creation(SessionsControllerTest)
[test/functional/sessions_controller_test.rb:13]:
<"ok, it exists"> expected but was
<"it does not exist">.
1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
It seems that User.new is not persisting to the database. But why? I have
asserted the persistion and it passes.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/SvJmSUO2048J.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Apparently Analagous Threads
- Ruby on Rails Tutorial Chapter 8 Signup Success
- Problem with implicit multipart emails using ActionMailer in Rails 2.3.3
- Getting rspec error: Net::SMTPServerBusy: Relay access denied
- ActionMailer/SMTP: Net::SMTPSyntaxError when message body contains only a 'dot'
- Scoping issue with irf() from {vars}
