0

I'm new to feature tests and capybara, I have a real simple feature test right now and I can't get it to pass but I believe it's because of something I'm doing wrong writing the test. I'll post my test and let me know if anything jumps out at anyone and is a clear dev error. I think it's something I'm doing with the check?

require "rails_helper"

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

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

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!")
end

This is the error message I get.

Send a message Staff can send a message
 Failure/Error: expect(page).to have_content("Messages on their way!")
   expected to find text "Messages on their way!" in "Tulip Time Text Numbers Sent Messages Scheduled Messages Statistics staff@example.com Logout SMS Notifications Use this form to send SMS notifications to Visitors, Staff or Volunteers. Enter a Message: Groups: Group A Scheduled Text Message:"

Message.rb

def create
@message = Message.create(message_params)
if @message.save
  run_at_time = @message.send_at.present? ? @message.send_at : Time.zone.now
  people = Person.in_groups(message_params[:group_ids])
  if people.any?
    people.each do |person|
      person.delay(run_at: run_at_time).send_message(@message.body)
    end
    flash[:success] = "Messages on their way!"
  end
  redirect_to root_path
else
  render "new"
end
end

enter image description here

Bitwise
  • 8,021
  • 22
  • 70
  • 161
  • 2
    What makes you think the check is the issue? If the check was failing Capybara would throw an error on that line, you should check your test logs and check what parameters are being submitted – Thomas Walpole Mar 27 '16 at 04:34
  • Yeah, it's not the check but I still cannot figure out why it doesn't have the content I suggest. it passes all validations and by all means this code should pass but it just wont? – Bitwise Mar 27 '16 at 16:39
  • Post your controller code and the response HTML – Thomas Walpole Mar 27 '16 at 17:43
  • So when you call `Group.create!(name: "Group A")` is that actually creating people in that group, or just an empty group. From your controller action if the group is empty (people.empty? -> true) a flash message would not get set – Thomas Walpole Mar 28 '16 at 17:33
  • hmm.. I'll take a look and get back to you. – Bitwise Mar 28 '16 at 17:53
  • I'm going to guess it's not since you appear to just be calling the default rails create! on the class. You should probably look into using one of the factory libraries designed to help with building objects in tests - FactoryGirl, Machinist, etc.. – Thomas Walpole Mar 28 '16 at 17:56
  • You fixed it man! post it as an answer so I can give you points! – Bitwise Mar 28 '16 at 17:57
  • I will be using those libraries thanks – Bitwise Mar 28 '16 at 17:57

1 Answers1

1

The group you are creating as test data is empty so no flash message is being set. Rather than creating the objects by hand you probably want to look at using one of the factory libraries (FactoryGirl, Machinist, etc) to help in building objects without having to provide all the values every time, and also building required associations.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78