I am trying to use Shoulda to test my user class as followed:
user_test.rb
require 'test_helper'
include Devise::TestHelpers
class UserTest < Test::Unit::TestCase
should have_many(:holidays)
should have_many(:hopsital_bookings)
should have_and_belong_to_many(:roles)
should belong_to(:hospital)
end
User.rb
belongs_to :hospital
belongs_to :department
has_many :holidays
has_many :hospital_bookings
has_and_belongs_to_many :roles
When I run rake test I get the following output: Imgur.
Any ideas what I am missing here and why this is not working because it should!
It seems that you have to:
1) Put at your gem file:
group :test do
gem 'shoulda'
end
2) run bundle install
Related
I am getting an error when I'm running my test file.
factory.rb:334:in `factory_by_name': No such factory: user (ArgumentError)
I have the user model but still why am getting this error?
My factories.rb contains:
FactoryGirl.define do
factory :user do |u|
u.sequence(:email) {|n| "#{n}#email.com"}
u.login 'Joe'
u.password 'password'
u.password_confirmation 'password'
u.phone '1111111111'
u.gender 'F'
u.active '1'
u.social_profile_active '1'
end
end
in test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'factory_girl'
#Factory.find_definitions
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
#fixtures :all
# Add more helper methods to be used by all tests here...
end
my ruby version is 1.8.7 and rails3.0.1
And my gemfile looks like this:
group :test do
gem 'shoulda'
gem 'factory_girl_rails'
gem 'mocha'
end
Rename your directory to one of the following:
Factories can be defined anywhere, but will be automatically loaded if
they are defined in files at the following locations:
test/factories.rb
spec/factories.rb
test/factories/*.rb
spec/factories/*.rb
https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md
I have set up the request URL in my Twilio account to have it POST to: myurl.com/receivetext. It appears to be successfully posting because when I check the database using the Heroku console I see the following:
Post id: 5, body: nil, from: nil, created_at: "2012-06-14 17:28:01", updated_at: "2012-06-14 17:28:01"
Why is it receiving nil for the body and from attributes? I can't figure out what I'm doing wrong! The created and updated at are storing successfully but the two attributes that I care about continue to be stored as nil.
Here's the Receive Text controller which is receiving the Post request from Twilio:
class ReceiveTextController < ApplicationController
def index
#post=Post.create!(body: params[:Body], from: params[:From])
end
end
EDIT: When I dump the params I receive the following:
"{\"controller\"=>\"receive_text\", \"action\"=>\"index\"}"
I attained this by inserting the following into my ReceiveText controller. #params = Post.create!(body: params.inspect, from: "Dumping Params") and then opening up the Heroku console to find the database entry with from = "Dumping Params".
I simulated a Twilio request with a curl with the following command curl -X POST myurl.com/receivetext route -d 'AccountSid=AC123&From=%2B19252411234'
I checked the production database again and noticed that the curl request did work when obtaining the FROM attribute. It stored the following:
params.inspect returned "{\"AccountSid\"=>\"AC123\", \"From\"=>\"+19252411234\", \"co..."
I received a comment stating: "As long as twilio is hitting the same URL with the same method (GET/POST) it should be filling the params array as well" I have no idea how to make this comment actionable. I'm very new to rails.
Here's my database migration (I have both attributes set to string. I have tried setting it to text and that didn't work either) :
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :body
t.string :from
t.timestamps
end
end
end
Here is my Post model:
class Post < ActiveRecord::Base
attr_accessible :body, :from
end
Routes (everything appears to be routing just fine) :
MovieApp::Application.routes.draw do
get "receive_text/index"
get "pages/home"
get "send_text/send_text_message"
root to: 'pages#home'
match '/receivetext', to: 'receive_text#index'
match '/pages/home', to: 'pages#home'
match '/sendtext', to: 'send_text#send_text_message'
end
Here's my gemfile (incase it helps)
source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'badfruit'
gem 'twilio-ruby'
gem 'logger'
gem 'jquery-rails'
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
Wow! I finally figured out why this wasn't working. It turns out that I created a new "APP" in my Twilio account and was setting the POST destination URL in this additional app that I created. The issue with this turned out to be the fact that I was using a Twilio sandbox trial account and I needed to paste the POST url into that sandbox entry field in the "Test your app" section.
I have basically a bare-bones rails 3.1 application that I want to deploy to heroku. I have followed this tutorial to use Devise, Cancan, and Rolify. (I added a username column to Users table)
Everything is working just fine on my development server, but when I try to run heroku run rake db:migrate I get the error "rake aborted! uninitialized constant Rolify::Roles"
Here is my user.rb file
class User < ActiveRecord::Base
include Rolify::Roles
has_and_belongs_to_many :roles, :join_table => :users_roles
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
end
I have tried running heroku restart then trying the migration again, but no good.
change include Rolify::Roles to rolify
So, I had already solved this question and forgot to post the answer here. I had been using an older version of rolify in my dev env than what heroku was using, so I ran bundle update and followed the update instructions on rolify git page and all was well.
How to organize rspec 2 tests into 'unit' (fast) and 'integration' (slow) categories?
I want to be able to run all unit tests with just rspec command, but not the 'integration' tests.
I want to be able to run only 'integration' tests.
We have groups of the same nature.
We run then one by one both on the local dev boxes and on the CI.
you can simply do
bundle exec rake spec:unit
bundle exec rake spec:integration
bundle exec rake spec:api
This is what our spec.rake looks like
namespace :spec do
RSpec::Core::RakeTask.new(:unit) do |t|
t.pattern = Dir['spec/*/**/*_spec.rb'].reject{ |f| f['/api/v1'] || f['/integration'] }
end
RSpec::Core::RakeTask.new(:api) do |t|
t.pattern = "spec/*/{api/v1}*/**/*_spec.rb"
end
RSpec::Core::RakeTask.new(:integration) do |t|
t.pattern = "spec/integration/**/*_spec.rb"
end
end
One way to do it is to tag your RSpec test cases like this:
it "should do some integration test", :integration => true do
# something
end
When you execute your test cases use this:
rspec . --tag integration
This will execute all the test cases with the tag :integration => true. For more refer to this page.
I had to configure my unit and feature tests as follows:
require 'rspec/rails'
namespace :spec do
RSpec::Core::RakeTask.new(:unit) do |t|
t.pattern = Dir['spec/*/**/*_spec.rb'].reject{ |f| f['/features'] }
end
RSpec::Core::RakeTask.new(:feature) do |t|
t.pattern = "spec/features/**/*_spec.rb"
end
end
Had to add require 'rspec/rails' and change Rspec to RSpec in the answer given by #KensoDev.
Notice at https://github.com/rspec/rspec-rails, they are telling you to place the gem under "group :development, :test" like so,
group :development, :test do
gem 'rspec-rails', '~> 2.0'
end
but if you place this only under :test group only,
group :test do
gem 'rspec-rails', '~> 2.0'
end
then you'll get the above error.
HTH
I suggest to use .rspec file to configure patterns instead of using rake because it's tricky to pass flags to RSpec when using rake.
You can read environment variables in your .rspec file:
<%= if ENV['TEST'] == 'integration' %>
--pattern spec/integration/**/*_spec.rb
<% else %>
<% ENV['TEST'] = 'unit' %>
--pattern spec/unit/**/*_spec.rb
<% end %>
Then you can run TEST=integration rspec to run integration tests or just rspec to run unit tests. The advantage of this approach is that you can still pass flags to it like:
TEST=integration rspec -t login -f doc
I'm having a hard time getting a simple file upload test working. I'm using Rails 3.0.0 on ruby 1.9.2 with Cucumber and Capybara.
View:
<%= form_tag "/upload/create", :multipart => true do %>
<label for="file">File to Upload:</label>
<%= file_field_tag "file" %>
<%= submit_tag "Upload" %>
<% end %>
Cucumber Step:
When /^I upload the basic file$/ do
visit path_to("upload")
path = File.join(::Rails.root, "somefile")
attach_file("file", path)
click_button("Upload")
end
In my controller, i have commented out everything except for:
def create
file = params[:file]
end
Gemfile snippet:
group :development, :test do
# testing with specs
gem "ZenTest", ">= 4.3.3"
gem "autotest"
gem "rspec-rails", ">= 2.0.0.beta.19", :git => "git://github.com/rspec/rspec-rails.git"
gem "rspec", :git => "git://github.com/rspec/rspec.git"
gem "rspec-core", :git => "git://github.com/rspec/rspec-core.git"
gem "rspec-expectations", :git => "git://github.com/rspec/rspec-expectations.git"
gem "rspec-mocks", :git => "git://github.com/rspec/rspec-mocks.git"
# cucumber stuff
gem 'capybara'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'cucumber'
gem 'spork'
gem 'launchy' # So you can do Then show me the page
gem 'escape_utils' # needed to fix Cucumber - http://crimpycode.brennonbortz.com/?p=42
end
When I try to run the test, I receive:
(::) failed steps (::)
bad content body (EOFError)
<internal:prelude>:10:in `synchronize'
I appreciate any help or insight. Thanks.
This turned out to be an issue with rack-test and it probably won't be a problem for most until more people adopt Rails3 and Ruby 1.9.x.
Upgrading rack-test to the current master branch fixed the problem.
I'm not sure when rack-test will include these changes in the gem.
See also:
groups.google.com/group/cukes/browse_thread/thread/5028306893c2c54a
I don't have an answer but working on the same problem in the same environ - cukes, capybara, rails 3, 1.9.2.... if I figure this out will let you know. Have you thought of posting on the cucumber google group or the Rails google group? If you don't once I get my act together and cant figure out will post to one of these.
Also, it seems that webrat has the method for attach_file() and thus when I generated cucumber without capybara it had a corollary method in web_steps.rb, but after I added capybara and regenerated cucumber it was gone....