Shoulda Test in Rails 3: should respond_with :success - ruby-on-rails

I have some troubles with testing in Rails 3. I'm currently upgrading a Rails2 app to Rails3. I'm using shoulda for testing. In my functional tests, I'm testing with shoulda, that a GET should respond with success
context "GET to :blame" do
should "mark a song as blamed" do
get :blame, :id => #song.id
assert_equal Blame.count, 1
get :blame, :id => #song.id
assert_equal Blame.count, 2
end
should respond_with :success
end
In the next to last line I'm getting the following error while executing the functional tests with rake test:functionals:
1) Error:
test: a visitor GET to :blame should respond with 200. (SongsControllerTest):
NoMethodError: undefined method `response_code' for nil:NilClass
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:57:in `response_code'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:48:in `correct_status_code?'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:30:in `matches?'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/assertions.rb:53:in `assert_accepts'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:324:in `block in should'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `call'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash'
I'm using Ruby 1.9.2, Rails 3.0.3 and Shoulda 2.11.3.
Hope, someone can help me.
thx,
tux

The two should blocks are run separately and no request would be made for respond_with hence the error. You would need to make the request in a setup block such as:
context "GET to :blame" do
setup do
get :blame, :id => #song.id
end
should "mark a song as blamed" do
assert_equal Blame.count, 1
end
should respond_with :success
end

Related

RoR rspec-rails error NoMethodError: Undefined method ‘get’

everybody!
I am doing RoR requests test and I am getting this error:
Failures:
1) Users GET #index is a success
Failure/Error: before(:example) { get "/users" }
NoMethodError:
undefined method `get' for #<RSpec::ExampleGroups::Users::GETIndex:0x000055a33f143f60>
Did you mean? gets
gem
# ./spec/requests/users_spec.rb:6:in `block (3 levels) in <top (required)>'
My code is:
RSpec.describe 'Users', type: :request do
describe 'GET #index' do
before(:example) { get "/users" }
it 'is a success' do
expect(response).to have_http_status(:ok)
end
it "renders 'index' template" do
expect(response).to render_template(:index)
end
it "shows the rendered text 'Welcome to the Ruby on Rails Blog'" do
expect(response).to include('Welcome to the Ruby on Rails Blog')
end
end
end
I did not include require ‘rails_helper’ in the spec file, because when I do this I am getting this error
ChildProcess::MissingFFIError:
FFI is a required pre-requisite for Windows or posix_spawn support in the ChildProcess gem. Ensure the `ffi` gem is installed. If you believe this is an error, please file a bug at http://github.com/enkessler/childprocess/issues
ruby version => ruby 3.1.1p18
rails version => Rails 7.0.2.4
RSpec version => RSpec 3.11

Why is my test framework unable to find a URL defined in my routes file?

I'm using Rails 5 and minutest and trying to write a test for a controller. The test is
# issues_controller_test.rb
class IssuesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test "logged in should get issues page" do
sign_in users(:one)
test_stop_id = 1
test_line_id = 1
get new_issue, :stop_id => test_stop_id, :line_id => test_line_id
assert_equal test_stop_id, #issue.stop_id
assert_equal test_line_id, #issue.line_id
assert_response :success
end
end
The method in question attempts to access this page, defined in my rails routes ...
new_issue GET /issues/new(.:format) issues#new
Yet when I run the test, I get this error
# Running:
.E
Error:
IssuesControllerTest#test_logged_in_should_get_issues_page:
NameError: undefined local variable or method `new_issue' for #<IssuesControllerTest:0x007f816759b330>
test/controllers/issues_controller_test.rb:10:in `block in <class:IssuesControllerTest>'
bin/rails test test/controllers/issues_controller_test.rb:6
Finished in 0.103956s, 19.2389 runs/s, 9.6195 assertions/s.
Why is the test framework unable to find a method defined in my routes file?
new_issue that way is just a local variable defined nowhere, try specifying it as a working url with new_issue_url:
test 'logged in should get issues page' do
sign_in users :one
test_stop_id = 1
test_line_id = 1
get new_issue_url, stop_id: test_stop_id, line_id: test_line_id
assert_equal test_stop_id, #issue.stop_id
assert_equal test_line_id, #issue.line_id
assert_response :success
end

Rspec not testing render, won't allow us to stub render action

Rails 5.0.0.1
Rspec 3.5.4
Ruby 2.3.1
We have been trying to provide test coverage for our rails application. We have a rescue in a private method that Rspec is not reaching.
Rspec:
it 'returns 200 after 404 from GET #edit error' do
allow(controller).to receive(:getpackages).and_return(URI::InvalidURIError)
expect(response.code).to eq(200) # => covers the 200
expect(response).to render_template('errors/5xx') # => doesn't read
end
Rails:
private
def set_package
#package = PackageServices.getpackage params[:id]
rescue URI::InvalidURIError
render 'errors/5xx'
end
Error message:
expecting <"errors/5xx"> but rendering with <[]>
./spec/controllers/packages_controller_spec.rb:139:in `block (3 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
We have tried to assert_template, tried to stub it using stub_template, installed a gem rails-controller-testing (not rspec), but we have run out of ideas and every google link is purple. Is this a bug in Rspec or are we going about it the wrong way?
I believe the stabbing was incorrect. Try the following code, it should work.
context 'URI is invalid' do
before do
allow(PackageServices).toreceive(:getpackage).and_raise(URI::InvalidURIError)
end
it 'returns 200 after 404 from GET #edit error' do
expect(response.code).to eq(200) # => covers the 200
expect(response).to render_template('errors/5xx') # => doesn't read
end
end

Error on ruby on rails test

I'm trying to execute this test:
def test_create
get :new
assert_template "admin/supplier/new"
assert_difference 'Supplier.count' do
post :create, :supplier => {:name => 'Juan', :province => 'provincia'}
assert_response :redirect
assert_redirected_to :action => 'index'
end
assert_equal 'Supplier Juan was succesfully created.', flash[:notice]
end
The following line throws an error:
assert_template "admin/supplier/new"
Here's the error:
test_create(Admin::SupplierControllerTest) [supplier_controller_test.rb:25]:
expecting <"admin/supplier/new"> but rendering with <"">
but <"admin/supplier/new"> doesn't work.
with some version, Rails stopped evaluating templates out of its "functional" tests.
You need to tell Rails to run the templates for this test. Either write a "view" test (which come with their own issues!).
The fix for RSpec is to add #render_views to your tests. I used the equivalent command for non-RSpec tests during my last project, so naturally I can't remember what it was now!

Rspec stub/mock with authentication token (authlogic)

first posting on this helpful community, but have been using it since the start of my programming journey. I'm new to rails and have trouble writing tests. So far, I've been trying to write a controller test based on the Authlogic perishable token guide .
But during the test, it keeps getting stuck on the line
#controller
#user = User.find_using_perishable_token(params[:activation_code], 1.week) || (redirect_to new_user_session_url and return)
and here's the line in the test that gets into trouble:
#test
User.any_instance.stub(:find_using_perishable_token) {mock_model(User, stubs(:active => true)) }
get :create, :activation_code => 'valid'
response.should render_template 'new'
response.code.should == "200"
#the test response is 302, not 200
response.should redirect_to("/")
#the test shows the path as user_sessions/new,
#meaning it didn't pass the condition in the controller with the find_using_pt method
Heres the test output:
Failure/Error: response.should render_template 'new' expecting <"new"> but rendering with <"">
# ./spec/controllers/activations_controller_spec.rb:18:in `block (3 levels) in <top (required)>'
After reading one of the posted answers ,it seems that I'm not creating the stub/mock correctly and that's why it keeps getting the redirect_to (302) rather than continuing with the test. (the find_using_pt is a method from Authlogic). Hence, the trouble is with the redirect_to line as it never continues with the controller/page render
Any advice would be greatly appreciated. Thanks.

Resources