rspec shoulda syntax for model.errors - ruby-on-rails-3

I get the following error message for the code below:
Failure/Error: #league.errors.on(:short_name).should_equal "can't be blank"
NoMethodError:
undefined method `on' for #<ActiveModel::Errors:0x000001017853f0>
#league.errors.on(:short_name).should_equal "can't be blank"
According to several documentation references that I am reading, this should work. Any idea what's going on?

Since #location.errors is a hash:
require 'rspec'
describe "Location" do
before(:each) do
#location = double("Location")
#location.stub(:errors).and_return(:short_name => "can't be blank")
end
it "should work like a hash" do
#location.errors[:short_name].should == "can't be blank"
end
end

Related

display error messages without brackets in rails api

render( json: UserSerializer.response_error(current_user.errors.messages).to_json)
user_serializer.rb
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email
def self.response_error(error)
error
end
end
It gives the response as below:
[
"Password can't be blank"
]
But I need to display
"Password can't be blank"
I tried to use "full_messages" but the result was same
def self.response_error(error)
error.join(', ')
end
You could do it like this. The issue is it needs to be an array. Since there could be multiple errors like.
[
"Email can't be blank",
"Password can't be blank"
]
This will be result with the above code in:
"Email can't be blank, Password can't be blank"
In general an array is easier to work with in the frontend regardless of one or many errors.
When you are absolutely sure there will only this one error you could also do
error.join('') to just get the string.

`permitted_to?` in helper causing exception with rspec using `declarative_authorization`

I am writing rspec for helper. But getting error while using `permitted_to?' in helper.
It works fine in application but in test case it causing error.
Code
module PostModule
module ApplicationHelper
def link_for_post(post)
if can_edit_post?(post)
edit_post_path(post)
else
posts_path
end
end
def can_edit_post?(post)
permitted_to?(:edit, post, :context => :post_module_posts)
end
end
end
Rspec
describe PostModule::ApplicationHelper do
describe '#link_for_post' do
let(:user) { FactoryGirl.create(:user) }
let(:first_post) {stub_model(PostModule::Post, {id: 1 })}
it "should return edit link if has permission" do
can_edit_post?(post).and_return(true)
link_for_post(post).should eq('/posts/1/edit')
end
it "should return index path" do
can_edit_post?(post).and_return(false)
link_for_post(post).should eq('/posts')
end
end
end
Error
Failure/Error: can_edit_post?(post).and_return(true)
NoMethodError:
undefined method `permitted_to?' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x0000001209b8e0>

Testing Nested Resources Controller with RSpec - count doesn't change by 1

I have nested resources:
resources :portfolios do
resources :asset_actions
end
And following RSpec Controller: asset_actions_controller_spec.rb
before(:each) do
#portfolio = Factory(:portfolio)
end
describe "POST create" do
describe "with valid params" do
it "creates a new AssetAction" do
expect {
post :create, :asset_action => valid_attributes, :portfolio_id => #portfolio.id
##portfolio.asset_actions.create! valid_attributes #WORKS correctly, but this is Model
}.to change(#portfolio.asset_actions, :count).by(1)
end
end
end
While running Spec I got the following error:
Failure/Error: expect {
count should have been changed by 1, but was changed by 0
I can't find the reason for this failure. Any suggestions?
Notes: Rails 3.1.3, Ruby 1.9.3p5, RSpec 2.8.0
I think the problem is that #portfolio hasn't changed because it is a local variable. It's stored in memory and you've made changes to the database. So, you need to reload #portfolio to see it change. Try something like this:
describe "POST create" do
describe "with valid params" do
it "creates a new AssetAction" do
post :create, :asset_action => valid_attributes, :portfolio_id => #portfolio.id
expect { #portfolio.reload }.to change(#portfolio.asset_actions, :count).by(1)
end
end
end

factory_girl not playing nicely with rspec2 - ActiveRecord::AssociationTypeMismatch

Am using Rails 3.1.0rc8, Factory Girl 2.1.2, Factory Girl Rails 1.2.0, and RSpec 2.7.0.
I believe the error I am having is related to the problems discussed on this thread.
I have a spec that looks like this:
spec/integration/my_integration_spec.rb:
require 'spec_helper'
describe 'A Workflow' do
before(:all) do
#reseller = Factory(:reseller)
#product = Factory(:product, :reseller => #reseller)
end
describe 'A feature' do
it 'Does something' do
end
describe 'A sub-feature' do
before(:all) do
# Error!
#product.sold_at << Factory(:outlet, :reseller => #reseller)
end
it 'Does something' do
end
end
end
Running this spec causes an exception in the sub-feature:
Failure/Error: #product.sold_at << Factory(:outlet, :reseller => #reseller)
ActiveRecord::AssociationTypeMismatch:
Reseller(#90828680) expected, got Reseller(#59351220)
Interestingly, this error does not occur when I move the content of the nested before-hook into the primary before-hook.
require 'spec_helper'
describe 'A Workflow' do
before(:all) do
#reseller = Factory(:reseller)
#product = Factory(:product, :reseller => #reseller)
# No error!
#product.sold_at << Factory(:outlet, :reseller => #reseller)
end
describe 'A feature' do
it 'Does something' do
end
describe 'A sub-feature' do
it 'Does something' do
end
end
end
Would really appreciate any help in understanding this issue.
You can only have one before(:all) in each spec file. Use before(:all) at the top of a spec and thenbefore(:each)in a describe block. This is the reason that your second example works, you have removed the secondbefore(:all)`.
Also, be careful with before(:all). Any data created here will not be deleted from the database at the end of a spec, you will need to delete it in an after(:all) or use the database cleaner gem. See link for reasoning.

I am having trouble testing my controller's update action using Rspec, what am I doing wrong?

I am trying to test the failing branch of the update action on my controller but I am having trouble with the test. This is what I have and it fails on the last
describe "PUT 'article/:id'" do
.
.
.
describe "with invalid params" do
it "should find the article and return the object" do
Article.stub(:find).with("1").and_return(#article)
end
it "should update the article with new attributes" do
Article.stub(:update_attributes).and_return(false)
end
it "should render the edit form" do
response.should render_template("edit")
end
end
end
Any ideas as to why the last part fails to render the template?
You're splitting up the parts of your test incorrectly. Each it call is actually a new example and the state is reset before/after each one.
What you should be doing is:
describe "with invalid params" do
before do
#article = Article.create(valid_params_go_here)
end
it "should find the article and return the object" do
put :update, { :id => #article.id, :article => { :title => "" } }
response.should render_template("edit")
end
end
By doing it this way, the #article is set up before hand (although you could use a mock one if you really wanted to) and the request to the update action and the assertion that it actually renders the edit template all happen in the one example.
For people who are coming here in 2018, some updates(pun not intended) have been made. It's important to include "params" before listing the params. Additionally, you should use expect and not "should" since it will be deprecated in Rails 6.0.
describe "with invalid params" do
before(:each) do
#article = Article.create(valid_params_go_here)
end
describe "PATCH update/:id" do
it "should find the article and return the object" do
put :update, params: { id: #article.id, article: { title: "" } }
expect(response).to be_redirect
end
end