Cameron Bass Cameron Bass - 2 years ago 77
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: ""
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: [])
fill_in "Enter a Message:", with: "Test Message"
check "message_group_#{}"
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")

Here is my factory:

FactoryGirl.define do
factory :user do
email ""
password "password"

Simple right? What could be going wrong?

Answer Source
user = FactoryGirl.create(:user)
fill_in "Email", with:
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

  config.after :each do
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download