0
votes

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 == "prasad"
        #forget_pwd.token.should == "123456"
        forget_pwd.userkey = "14dd"
        visit auth_index_path
        click_link "Forgot Password"
        fill_in "Email", :with=> '[email protected]'
        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('[email protected]')
     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

1

1 Answers

0
votes

The challenge you're setting yourself is that you're trying to test too many things at once. You should test the model behavior in the model_spec and just test the flow behavior as you are doing within the controller. So your integration/request spec can test whether or not everything works together, and your User and ForgotPass unit testing can test whether or not when they receive the right message (from the controller) they do the right thing.

It's much easier breaking these tests down small. Unit testing the model methods do what you want means you can test the controller using stubs/mocks if you want to test it isolation, or you can just do the request spec testing as you are doing, confident the underlying model behavior is correct.