Cameron Bass Cameron Bass - 7 months ago 16
Ruby Question

How to use RSpec and FactoryGirl for simple sign in

I just started using factory_girl and I tried using:

user = FactoryGirl.create(:user)


but that doesn't seem to work. Here is my code:

RSpec.feature "Send a message" do
scenario "Staff can send a message" do
visit "/"

group = Group.create!(name: "Group A")
user = FactoryGirl.create(:user)
fill_in "Email", with: "staff@example.com"
fill_in "Password", with: "password"
click_button "Sign in"

person = Person.create!(groups: [group], phone_number: "+161655555555")
message = Message.create(body: "Test Message", group_ids: [group.id])
fill_in "Enter a Message:", with: "Test Message"
check "message_group_#{group.id}"
click_button "Send Message"

expect(page).to have_content("Messages on their way!")
expect(page).to_not have_content("Body can't be blank")
expect(page).to_not have_content("Group ids can't be blank")
end


Here is my factory:

FactoryGirl.define do
factory :user do
email "cam@example.com"
password "password"
end
end


Simple right? What could be going wrong?

Answer
user = FactoryGirl.create(:user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password

Also, if you use devise, you can add next lines to spec/support/warden.rb and use method login_as(user) instead of submitting login form.

RSpec.configure do |config|
  config.include Warden::Test::Helpers, type: :feature
  config.include Warden::Test::Helpers, type: :request

  config.before :suite do
    Warden.test_mode!
  end

  config.after :each do
    Warden.test_reset!
  end
end