how to tell Rails RSpec that spec is "type helper" - ruby-on-rails-3

I wrote *simple_form* input extension that is located in app/inputs/something_input.rb
I'm trying to write RSpec for this. When I put this spec inside spec/helpers/application_helper_spec.rb everything was working without single problem.
# spec/helpers/application_helper_spec.rb
require 'spec_helper'
describe ApplicationHelper do
it do
helper.simple_form_for #foo,:method=>'get', :url=>helper.users_path do |f|
f.input :created_at, :as =>:custom_datepicker
end.should =~ /something/
end
end
Now I'm trying to move that spec to spec/inputs/something_input_spec.rb so it will be similar name path.
# spec/imputs/something_input_spec.rb
require 'spec_helper'
describe SomethingInput do
it do
helper.simple_form_for #foo,:method=>'get', :url=>helper.users_path do |f|
f.input :created_at, :as =>:custom_datepicker
end.should =~ /something/
end
end
#
#ERROR: undefined local variable or method `helper' for #<RSpec::Core::ExampleGroup
the thing I want to tell RSpec to threat this file as type helper spec, so I will have helper method availible with all the RSpec::Rails::HelperExampleGroup functionality
... how can I do that ??
I was trying to extend/include it with RSpec::Rails::HelperExampleGroup nothing seems to work

Start the describe block with the :helper type:
describe SomethingInput, :type => :helper do
... tests ...
end

Related

FactoryGirl,Rspec2 and devise rails 3

I am using Rspec, FactoryGirl and Spork for my tests.There are 2 things I am a litte unclear on, first is the location of my factories.rb file. At present I have it located in
spec/support/factories.rb
And it looks like this
FactoryGirl.define do
factory :user do
email "example#yahoo.com"
password "password"
password_confirmation "password"
confirmed_at Time.now
end
end
Within my spec_helper I have
config.include FactoryGirl::Syntax::Methods
Secondly I want to login a user before starting my tests for a controller , this particular controller has a before filter :authenticate_user!
I am using devise for my authentication so have added
config.include Devise::TestHelpers, :type => :controller
Reading the devise docs you can add a controller_macros.rb and specify methods like so to use
def login_user
before(:each) do
#request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryGirl.create(:user)
user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
sign_in user
end
end
And so i added this also to my spec_helper
config.include ControllerMacros, :type => :controller
So when I add login_user before my controller tests i get undefined method login_user. Am i using two tools here to do the same thing? Do I actually need the devise methods or can it all be done with factoryGirl. If so how do i setup the login process before i can test a controller?
Factories location should be in spec/factories. Check out this example app https://github.com/RailsApps/rails3-devise-rspec-cucumber/tree/master/spec.
For login, generally you seems to doing it right. Check the example app again and here: https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29
For the undefined method login_user error be sure to have
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
and
config.extend ControllerMacros, :type => :controller
in spec_helper. Devise methods should be available wtih subject:
subject.current_user.should_not be_nil

File Handling with Paperclip

I'm using Paperclip in a gem I built for a specific use case. My gem creates an interface for non-programmers to create and edit forms and then allows users to answer those forms.
I want to use Paperclip in order to provide a "File Upload" input type for questions, so my forms are more versatile. However, this means that I need to use the file_field_tag method to display the file input and I need to manually save whatever information is submitted through that input into the appropriate model object. Currently I'm sending the information through with the name question_1 and then trying to pull the uploaded data out with params["question_1"].
My code looks like this:
answer.update_attributes(upload: params["question_1"])
But I'm getting a No handler found for <image_name> error and I can't figure out what I'm doing wrong. I thought Paperclip handles everything after I pass it the data from a file_field?
Solution:
My form looked like this: <%= form_for #answer_set, multipart: true do %> when it should have looked like this: <%= form_for #answer_set, html: { multipart: true } do %>.
I use
has_attached_file :image
validates_attachment_presence :image
validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png', 'image/jpg', 'image/pjeg']
and then:
#upload = Upload.find(params[:id])
#upload.update_attributes(params[:upload])
config/environment.rb
Rails::Initializer.run do |config|
config.gem "paperclip", version: "~> 2.7"
end
This thread also suggests checking the multi-part on the form
https://stackoverflow.com/a/10076046/1354978
answer_form_for #upload, :html => {:multipart => true} do |f|
There are also other possible solutions on that page.

Formtastic ~> 2.0.2 and enumerated_attribute gem, Rails 3.1.1

I used enumerated_attribute with formtastic ~> 1.2.3 with "monkey patch" for field :as => :enum and everything worked fine.
But when I updated formtastic to 2.0.2 version appeared an error with message "Formtastic::UnknownInputError".
For more details here is patch, that was added to /initialisers/formtastic.rb:
module Formtastic #:nodoc:
class SemanticFormBuilder #:nodoc:
def enum_input(method, options)
unless options[:collection]
enum = #object.enums(method.to_sym)
choices = enum ? enum.select_options : []
options[:collection] = choices
end
if (value = #object.__send__(method.to_sym))
options[:selected] ||= value.to_s
else
options[:include_blank] ||= true
end
select_input(method, options)
end
end
end
P.S. I tried to change SemanticFormBuilder to FormBuilder (as I understood from new formtastic documentation there was such change for all custom inputs), but I was still getting error
Maybe anybody already used these gems together successfully?
They way custom fields are defined has changed completely in Formtastic 2.x
You need to subclass the internal Formtastic classes to get what you want. A select input would look something like this:
module FormtasticExtensions
class EnumeratedInput < Formtastic::Inputs::SelectInput
def collection
# programmatically build an array of options in here and return them
# they should be in this format:
# [['name', 'value'],['name2', 'value2']]
end
end
end
Include the module in the Formtastic initializer:
include FormtasticExtensions
and this will give you a field :as => :enumerated and you should be good to go. In my case (some other custom field) it selects the current option, but you may need to tweak the code for yours to work.
You could also just pass the collection in:
f.input :thing, :as => :select, :collection => your_collection, :label_method => :your_name, :value_method => :your_id

Rails::Engine namespacing controller and models

I followed the following tutorial: http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/
And it all works great. I namespaced the controller using
#app/controller/authr/accounts_controller.rb
module Authr
class AccountsController < ApplicationController
unloadable
def new
#account = Account.new
end
def create
#account = Account.new(params[:account])
if #account.save
redirect_to '/'
else
render :action => :new
end
end
end
end
And in the tutorial he didn't namespace the model. I want to namespace my model though so it doesn't collide with host apps. So i tried the following:
#app/models/authr/account.rb
module Authr
class Account < ActiveRecord::Base
attr_accessor :password
validates_confirmation_of :password
end
end
This is my view, with a simple form_for that should go to accounts_path
#app/views/authr/accounts/new.html.erb
<%= form_for(#account) do |f|%>
<p>
<%= f.label :uname, "Username"%>
<%= f.text_field :uname%>
</p>
<p>
<%= f.label :password, 'Password'%>
<%= f.password_field :password%>
</p>
<p>
<%= f.submit "Submit"%>
</p>
<% end %>
But when i use my namespaced model i get the following error:
undefined method `authr_accounts_path' for #<#<class:0x1038f54e0>:0x1038f3780>
The object created by the new method (#account = Account.new) results in this :
<Authr::Account id: nil, uname: nil, hashed_password: nil, remember_token: nil, remember_expiry: nil, created_at: nil, updated_at: nil>
Routes file: (This works when i dont namespace the model.)
Rails.application.routes.draw do |map|
resources :accounts, :only => [:new, :create],
:controller => "authr/accounts"
end
So this is a routing thing. When i dont namespace the model all works fine but when i namespace it it doesnt work. Then i tried the following:
#routes.rb
Rails.application.routes.draw do |map|
scope "authr", :module => :authr, :as => "authr" do
resources :accounts
end
end
Now i get the form without the routing error. But when i try to submit the form the object isn't saved.
Started POST "/authr/accounts" for 127.0.0.1 at Mon Mar 28 18:51:12 +0200 2011
Processing by Authr::AccountsController#create as HTML
Parameters: {"commit"=>"Submit", "authenticity_token"=>"cPH8ZmNmgoT84UMnYBoM38di+/OZQmuGQTrSv3HhFR4=", "utf8"=>"✓", "authr_account"=>{"uname"=>"usrrrrrrrrrrrrnmmmmeee", "password"=>"[FILTERED]"}}
SQL (48.0ms) BEGIN
SQL (0.5ms) SHOW TABLES
SQL (13.2ms) describe `accounts`
AREL (0.3ms) INSERT INTO `accounts` (`updated_at`, `created_at`, `remember_expiry`, `uname`, `remember_token`, `hashed_password`) VALUES ('2011-03-28 16:51:12', '2011-03-28 16:51:12', NULL, NULL, NULL, NULL)
SQL (0.4ms) COMMIT
Redirected to http://localhost:3000/
I know that i'm doing #account = Account.new(params[:account]) and if i change it to Account.new(params[:authr_account] that i should work but i want to user params[:account] that should work right? Because the controller is namespaced as well...
Then i found something about isolated_name space so i tried this:
#lib/authr/engine.rb
require "authr"
require "rails"
module Authr
class Engine < Rails::Engine
isolate_namespace Authr
# engine_name :authr #deprecated?
end
end
and i changed my routes to:
Rails.application.routes.draw do |map|
resources :accounts, :only => [:new, :create],
:controller => "authr/accounts"
end
But this gives me the following error:
/Library/Ruby/Gems/1.8/gems/authr3-0.1.0/lib/authr/engine.rb:6: undefined method `isolate_namespace' for Authr::Engine:Class (NoMethodError)
I tried everything and i looked at other gems and they have namespaced models. I am convinced that i need to namespace my models just to be sure that they don't conflict with the host application. I want to use restfullroutes but i don't know how i can fix this problem.
I am using:
Daniel-Zs-MacBook-Pro:gem_test Daniel$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
Daniel-Zs-MacBook-Pro:gem_test Daniel$ rails -v
Rails 3.0.3
Thanks for any advice / help
Possibly a typo?
scope "authr", :module => :authr, :as => "auth" do
change to
scope "authr", :module => :authr, :as => "authr" do #you are missing an r
If its just a typo in this post and you have it correct in the engine, then what do you get when you run "rake routes" from the parent application using that same scope in the engine?
Also, I think isolate_namespace is only in edge rails right now. 3.1 is slated to have alot of new engine goodies including this.

Undefined name method test error in Rails3

Working through Michael Hartl's RailsTutorial and came across the following error - even though I have followed everything to the 'T'.
1) UsersController GET 'index' for signed-in users should have an element for each user
Failure/Error: response.should have_selector("li", :content => user.name)
undefined method `name' for #<Array:0x000001032c07c8>
Did anyone else get a similar error and know how to fix it?
I am in Chapter 10.
Btw, when I try the page it does what it is supposed to do. It's just that the test fails in RSpec.
FYI, here is the related test code from the users_controller_spec.rb
require 'spec_helper'
describe UsersController do
render_views
describe "GET 'index'" do
describe "for non-signed-in users" do
it "should deny access" do
get :index
response.should redirect_to(signin_path)
flash[:notice].should =~ /sign in/i
end
end
describe "for signed-in users" do
before(:each) do
#user = test_sign_in(Factory(:user))
second = Factory(:user, :email => "another#example.com")
third = Factory(:user, :email => "another#example.net")
#users = [#user, second, third]
end
it "should be successful" do
get :index
response.should be_success
end
it "should have the right title" do
get :index
response.should have_selector("title", :content => "All users")
end
it "should have an element for each user" do
get :index
#users.each do |user|
response.should have_selector("li", :content => user.name)
end
end
end
end
My spec/spec_helper.rb file looks like the following:
require 'rubygems'
require 'spork'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
ENV["RAILS_ENV"] ||= 'test'
unless defined?(Rails)
require File.dirname(__FILE__) + "/../config/environment"
end
require 'rspec/rails'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
Rspec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, comment the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
### Part of a Spork hack. See http://bit.ly/arY19y
# Emulate initializer set_clear_dependencies_hook in
# railties/lib/rails/application/bootstrap.rb
ActiveSupport::Dependencies.clear
def test_sign_in(user)
controller.sign_in(user)
end
def integration_sign_in(user)
visit signin_path
fill_in :email, :with => user.email
fill_in :password, :with => user.password
click_button
end
end
end
Spork.each_run do
end
it appears your test_sign_in method is returning an instance of an array rather than a User object. Are you explicitly returning a user object in the test_sign_in method? If not, have a look at the last line that's executed in that method, I have a feeling the result of it is an array.
I solved this issue, and the answer can be found on the railstutorial official forums.