[rspec-users] Testing Controllers in Rspec
I am building an Rails application where the forgot password feature is implemented. I need to write the test cases for it using RSpec. I am very new to RSPEC and got the basic idea of how rspec works from the tutorial and screencasts. But I am not sure how it can be tested for controllers. In my controller method if i am retrieving a user from database, then how would I test it using rspec. Do I give dummy data for testing rspec.?? Please find the example below. In Usercontroller.rb def forgot_pass user = User.find_by_email(params[:email]) forget_pass = ForgetPassword.new forgetpass.userid = user.email forgetpass.status = true forgetpass.save! redirect_to login_url end In my password_reset_spec require 'spec_helper' describe "PasswordResets" do it "emails when user requesting password reset" do user=User.new forget_pwd = ForgetPassword.new #forget_pwd.userid.should == "pradeep" #forget_pwd.token.should == "123456" forget_pwd.userkey = "14dd" visit auth_index_path click_link "Forgot Password" fill_in "Email", :with=> 'pradeep83.a...@gmail.com' click_button "Submit" current_path.should eq(login_path) page.should have_content("A verification mail has been sent to your email id. Click on the confirmation link to change the password") last_email.to.should include('pradeep83.a...@gmail.com') end end In the test how do I test whether the user exists and assign the attributes. In my case, user = User.find_by_email(params[:email]) forgetpass.userid = user.email Please help as I am newbie in Rspec -- Posted via http://www.ruby-forum.com/. ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Testing Controllers in Rspec
guirec c. wrote in post #1099350: > I think your are confused with acceptance specs and controllers spec. In > a controller spec you must to test only your controller in total > isolation. You can use stubs and mocks to do it. In the acceptance spec > you must to test all the behaviour at a very high level. > > I think to create a record in the database for a feature spec is > correct. You can see an example of my specs here : > https://github.com/GCorbel/comment-my-projects/blob/master/spec/acceptance/actualities_spec.rb > and here : > https://github.com/GCorbel/comment-my-projects/blob/master/spec/controllers/actualities_controller_spec.rb. > > As you can see, I use acceptance DSL > (http://jeffkreeftmeijer.com/2011/acceptance-testing-using-capybaras-new-rspec-dsl/) > and factory girl (https://github.com/thoughtbot/factory_girl_rails). > > I hope to help you. It's very hard to begin. >From the link above you provided for acceptance testing I understand that we write test cases for failing conditions first and then write the code until the tests are passed. But one question that arise in my mind is that in acceptance testing do we need to write tests for checking blank values, no or no of chars for password field (like validations)?? For eg, if I am writing test cases for Forgot password feature, then I write test cases as above what I have written. So as per the test cases I would write code in controllers/models/views and make the test pass. But do I also need to write test cases for scenarios like if Email field is left blank and clicked submit button, then it should fail?? -- Posted via http://www.ruby-forum.com/. ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
[rspec-users] uninitialized constant Factory (NameError)
I have installed factory_girl_rails gem in Gemfile for both test as well as development. And in the password_rest_spec file I have given require 'spec_helper' describe "PasswordResets" do it "emails when user requesting password reset" do user = Factory(:user) #forget_pwd.userid.should == "ruby" #forget_pwd.token.should == "123456" #forget_pwd.userkey = "14dd" visit auth_index_path click_link "Forgot Password" fill_in "Email", :with=> user.email click_button "Submit" current_path.should eq(login_path) page.should have_content("Hello Rails") last_email.to.should include(user.email) end end I have created another file called factories.rb where I have created the data as below. Factory.define :user do |f| f.sequence(:email) {|n| "foo#{n}@example.com"} f.password "Secret" end But when I am running this spec, it is failing and showing the error as uninitialized constant Factory (NameError). I have followed exactly as in the railscasts. http://railscasts.com/episodes/275-how-i-test Please help -- Posted via http://www.ruby-forum.com/. ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
[rspec-users] Validation Failed: Userkey has already been taken, Email has already been taken
Hi, I am using Factory girl with rspec and capybara for testing my rails appliction. I have the below code FactoryGirl.define do factory :user do |f| f.email "s...@gmail.com" f.userkey "12ssd345q62" end end When I run the test it is failing as "Validation Failed: Userkey has already been taken, Email has already been taken". What could be the reason for this. I am new to Rspec. -- Posted via http://www.ruby-forum.com/. ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users