Skip to content

Instantly share code, notes, and snippets.

@shotty01
Last active December 15, 2015 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shotty01/5318831 to your computer and use it in GitHub Desktop.
Save shotty01/5318831 to your computer and use it in GitHub Desktop.
view spec for the create form of a user. it's passing. not using factory_girl and devise. Maybe that's wrong? previous version was webrat syntax. this version is capybara syntax. refactored a bit
require 'spec_helper'
require 'shared_examples_for_views'
describe "users/new" do
#generalized method to check a field
def check_field(form, id, options={})
field = form.find_field(id)
options.keys.each do |key|
field[key].should eq options[key].to_s
end
end
let(:user) do
# mock it? stub it? use factory girl? so many options..
User.new
#mock_model(User).as_new_record.as_null_object
end
context "with 2 roles present in DB" do
let(:roles) do
[create(:admin_role), create(:role)]
end
before do
assign(:user, user)
#i have to add this, or the roles are not present in DB...
assign(:roles, roles)
# stub user as an admin
# should I do this with devise and factory_girl instead?
login_user = double('User')
login_user.stub(:role?).with(:admin).and_return(true)
controller.stub(:current_user) { login_user }
render
end
# path for cancel and form
let(:path) { users_path}
let(:object) { user}
it_behaves_like "valid form"
# maybe I should split this up in own tests
it "renders the user fields" do
find_form(rendered) do |form|
check_field(form, "user_login", :type => :text, :name => "user[login]", :placeholder => 'Login')
check_field(form, "user_firstname", :type => :text, :name => "user[firstname]")
check_field(form, "user_lastname", :type => :text, :name => "user[lastname]")
check_field(form, "user_ifxglobalid", :type => :text, :name => "user[ifxglobalid]")
roles.each do |role|
check_field(form, "user_role_ids_#{role.id}", :type=> :checkbox, :name => "user[role_ids][]", :value => role.id)
end
end
end
end
end
def find_form(rendered)
Capybara.string(rendered).find("form").tap do |form|
yield form
end
end
shared_examples "valid form" do
it "should render form" do
rendered.should have_selector("form[action=\"#{path}\"][method=\"post\"]")
end
it "should render submit button" do
find_form(rendered) do |form|
input = form.find_button("Create #{object.class.name.capitalize}")
input[:type] == "submit"
# submit button
#form.should have_selector("input[type=\"submit\"]")
end
end
it "should render cancel button" do
find_form(rendered) do |form|
# cancel button
#form.should have_selector("a[href=\"#{path}\"]")
link = form.find_link("Cancel")
link[:href] == path
end
end
end
@ruralocity
Copy link

Like I commented elsewhere, I don't like to write specs at this level. But I think you're on the right track with using mocks/stubs in this context, since it's more unit test-y. Anything you can do to abstract from Devise in particular at this level is beneficial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment