Last active
December 15, 2015 20:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.