I am doing the thoughtbot intro to testing program. Im not sure how to test for what they want.
Below is my test.
require "rails_helper"
describe PeopleController do
describe "#create" do
context "when person is valid" do
it "redirects to #show" do
post :create, FactoryGirl.build_stubbed(:person)
expect(response).to redirect_to(show_people_path)
end
end
context "when person is invalid" do
it "redirects to #new" do
pending "create this test"
end
end
end
end
I am of course using factory girl. I have tried several methods. I really don't know hoe to test this controller.
Any insights would be great.
I would create an 'invalid' person using the FactoryGirl, and send it as a parameter to the post :create.
To create an invalid person record, why don't you use nested factories in FactoryGirl? Depending on the validation in your model, you can simply do something like:
spec/factories/person.rb
FactoryGirl.define do
factory :person do
...
factory :invalid_person do
...
email nil
...
end
end
end
in your test
context "when person is invalid" do
it "redirects to #new" do
post :create, FactoryGirl.build_stubbed(:invalid_person)
expect(response).to redirect_to action: :new
end
end
Related
My application manages BusinessFlows, which can only be created within a parent BusinessArea. The 'new' method in BusinessFlows controller is:
def new
#business_area = BusinessArea.find(params[:business_area_id])
#business_flow = BusinessFlow.new
end
The Factory used for testing works fine, and creates the parent BusinessArea, and the the BusinessFlow as expected:
FactoryBot.define do
factory :business_flow do
association :parent, factory: :business_area
playground_id {0}
name {"Test Business Flow"}
code {Faker::Code.unique.rut}
description {"This is a test Business Flow used for unit testing"}
created_by {"Fred"}
updated_by {"Fred"}
owner_id {0}
responsible_id {0}
deputy_id {0}
organisation_id {0}
status_id {0}
end
end
But when it comes to test if the 'new' view can be displayed, the controller raises an error due to missing params[:business_area_id]:
ActiveRecord::RecordNotFound:
Couldn't find BusinessArea without an ID
Here is the feature test script:
require 'rails_helper'
RSpec.describe BusinessFlow, type: :request do
include Warden::Test::Helpers
describe "Business Flows pages: " do
let(:bf) {FactoryBot.create(:business_flow)}
context "when not signed in " do
it "should propose to log in when requesting index view" do
get business_flows_path
follow_redirect!
expect(response.body).to include('Sign in')
end
it "should propose to log in when requesting new view" do
get new_business_flow_path
follow_redirect!
expect(response.body).to include('Sign in')
end
it "should propose to log in when requesting edit view" do
get edit_business_flow_path(bf)
follow_redirect!
expect(response.body).to include('Sign in')
end
it "should propose to log in when requesting show view" do
get business_flow_path(bf)
follow_redirect!
expect(response.body).to include('Sign in')
end
end
context "when signed in" do
before do
get "/users/sign_in"
test_user = FactoryBot.create(:user)
login_as test_user, scope: :user
end
it "should display index" do
get business_flows_path
expect(response).to render_template(:index)
end
it "should display new view" do
get new_business_flow_path(bf.parent.id)
expect(response).to render_template(:_form)
end
it "should display edit view" do
get edit_business_flow_path(bf)
expect(response).to render_template(:_form)
end
it "should display show view" do
get business_flow_path(bf)
expect(response).to render_template(:show)
end
end
end
end
Only the 'new' method test in the context 'when signed in' fails.
Can you please help me solving this?
Thanks a lot!
it "should display new view" do
get new_business_flow_path(business_area_id: bf.parent)
expect(response).to render_template(:_form)
end
Rails thinks the id you're passing is part of the business flow path (and it isn't), it needs to be passed as a param I think.
I'm new in testing and learning Rspec, and I can't git it working.
(I have read the book Effective testing with Rspec3, and many tutorials ...also pluralsight.com)
The situation is very simple. In a Companies controller I want to test de Create method, the company model belongs_to user, and is this the code:
I think the problem is when execute
in test: expect(Company).to receive(:new).with(company_params)
or in controller: #company.user=helpers.user
Controller:
class CompaniesController < SessionsController
def create
#company=Company.new(company_params)
#company.user=helpers.user
if #company.save()
redirect_to companies_path
else
render :edit
end
end
and Rspec:
RSpec.describe CompaniesController, type: :controller do
let(:user) { instance_double(User) }
before do
allow_any_instance_of(ApplicationHelper).to receive(:user){user}
allow(controller).to receive(:authorize){true}
end
describe 'Authenticated user with companies' do
let(:company_params) { {company:{name:"Albert",domain:"www.albert.com"}} }
let(:company) { instance_double(Company) }
before do
allow(Company).to receive(:new){company}
end
describe 'POST #create' do
context "with valid data" do
before { allow(company).to receive(:save){true} }
it "redirects to companies_path" do
expect(Company).to receive(:new).with(company_params)
expect(company).to receive(:user=).with(user)
post :create, params:{company: company_params}
expect(response).to redirect_to(companies_path)
end
My intention is very simple: Use instance_double to mock (or stub) #company, and Company.new, using instance double...to test the create action, and simulate the "save()" returning true...etc
I do not know if I explain myself very well, but given the create action of controlloer , how to test using mocks ans stubs, instance_double?
Thanks
First of all let me explain what we need to test here
def create
#company=Company.new(company_params)
#company.user=helpers.user
if #company.save()
redirect_to companies_path
else
render :edit
end
end
We are testing create action of a controller. First let us see what this action does? It's just takes comapany_params as input and create a company record in database.
Testing also goes like the same, we need to just pass the input that action required, and need to check whether it's creating record in database or not.
RSpec.describe CompaniesController, type: :controller do
let(:user) { instance_double(User) }
before do
# all your authentication stubing goes here
allow_any_instance_of(ApplicationHelper).to receive(:user){user}
allow(controller).to receive(:authorize){true}
end
describe 'POST#create' do
context 'with valid attributes' do
before do
post :create, { company:{ name:"Albert", domain:"www.albert.com"} }
end
it 'responds with success' do
expect(response.status).to eq(302)
end
it 'creates company' do
company = Company.find_by(name: "Albert")
expect(assigns(:company)).to eq(company)
expect(response).to redirect_to(companies_path())
end
end
context 'with invalid attributes' do
before do
post :create, { company:{ name:"", domain:""} }
end
it 'renders new template' do
expect(response).to render_template(:edit)
end
end
end
end
No need to sub anything here. As per my knowledge, Only when we use any lib classes / background jobs / third party libraries code inside action then we need to stub those code. Because for all those, we will write specs separately. So no need to test again here that's why we'll do stubing.
Thanks to Narsimha Reddy, I have better ideas about how to test.
Eventhough, if I want to stub
#company=Company.new(company_params)
#company.user=helpers.user
if #company.save()
For testing only de create's response , the solution was in a good use of parameters, and allowing allow(company).to receive(:user=) for the belongs_to association
let(:company_params) {{company:{name:"Albert",domain:"www.albert.com"}}}
let(:ac_company_params) {ActionController::Parameters.new(company_params).require(:company).permit!}
let(:company) { instance_double(Company) }
before do
allow(Company).to receive(:new){company}
allow(company).to receive(:user=)
allow(company).to receive(:save){true}
end
it "redirects to companies_path" do
expect(Company).to receive(:new).with(ac_company_params)
expect(company).to receive(:user=).with(user)
post :create, params: company_params
expect(response).to redirect_to(companies_path)
end
Using Rails 4.2, rspec 2.14, rspec-rails 2.14, faker and factory-girls-rails gems
I have a model called Appointment that I'm running some tests on and everything passes except for the #create under the controller spec.
The error message I get is:
Failure/Error: post :create, FactoryGirl.attributes_for(:appointment)
ActionController::ParameterMissing:
param is missing or the value is empty: appointment
The Appointment model validates the presence of an association to an object called Service.
Here is my factory for appointment.rb:
require 'faker'
FactoryGirl.define do
factory :appointment do |f|
f.service {FactoryGirl.create(:service)}
f.appointment_time { Faker::Time.between(DateTime.now - 1, DateTime.now) }
end
end
Here is my appointment_spec.rb:
require 'spec_helper'
describe Appointment do
it "has a valid factory" do
FactoryGirl.create(:appointment).should be_valid
end
it "is invalid if it does not have a Service association" do
FactoryGirl.build(
:appointment, service: nil).should_not be_valid
end
end
I've been following the instructions listed here for making my Controller Spec. I've also found a lot of stackoverflow posts that say to do the same thing, yet I still get the same error.
Here are the tests not passing from my appointment_controller_spec.rb
describe AppointmentsController do
#other controller action code...
describe "POST #create" do
context "with valid attributes" do
it "saves the new appointment in the database" do
expect {
post :create, FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end
it "redirects to show page" do
post :create, FactoryGirl.attributes_for(:appointment)
response.should redirect_to Appointment.last
end
end
end
I'm at a loss and hoping some one can offer some insight.
EDIT:
As some of you had recommended, I changed the controller spec. This is actually what I had before I changed my code to what you see above:
it "saves the new appointment in the database" do
expect {
post :create, appointment: FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end
The reason I changed this is because when I had this my original error message was:
Failure/Error: expect {
count should have been changed by 1, but was changed by 0
Sorry for the confusion.
I believe you just need your AppointmentsController spec to read as follows:
describe AppointmentsController do
#other controller action code...
describe "POST #create" do
context "with valid attributes" do
it "saves the new appointment in the database" do
expect {
post :create, appointment: FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end
it "redirects to show page" do
post :create, appointment: FactoryGirl.attributes_for(:appointment)
response.should redirect_to Appointment.last
end
end
end
Adding appointment: before you supply the attributes via FactoryGirl in the post call.
Are you using strong_params in your controller? It looks like you are looking for an appointment param, but you are just getting a hash of the attributes.
Try this:
it "saves the new appointment in the database" do
expect {
post :create, appointment: FactoryGirl.attributes_for(:appointment)
}.to change(Appointment, :count).by(1)
end
So if your #create controller method is simply:
#testimonial = Testimonial.new(testimonial_params)
And you test it in your specs like so:
testimonials_controller_spec.rb
describe "POST #create" do
context "with VALID attributes" do
it "creates new testimonial" do
expect {
post :create, testimonial: FactoryGirl.attributes_for(:testimonial)
}.to change(Testimonial, :count).by(1)
end
end
end
It works fine. The code:
post :create, testimonial: FactoryGirl.attributes_for(:testimonial)
is correct.
However, in my TestimonialsController, my create method is actually:
#testimonial = current_user.testimonials.build(testimonial_params)
My rspec method doesn't work with this. What should I use instead of:
post :create, testimonial: FactoryGirl.attributes_for(:testimonial)
?
Sign in the user before calling the controller action. Please find the following:
#testimonials_controller_spec.rb
require 'rails_helper'
describe TestimonialsController, type: :controller do
let(:user) do
FactoryGirl.create :user
end
before do
sign_in user
end
describe "POST #create" do
context "with VALID attributes" do
it "creates new testimonial" do
expect {
post :create, testimonial: FactoryGirl.attributes_for(:testimonial)
}.to change(Testimonial, :count).by(1)
end
end
end
end
Build doesn't save/persist the record to the database. Why not make it:
#testimonial = current_user.testimonials.new(testimonial_params)
#testimonial.save
I've got the following factory:
FactoryGirl.define do
factory :category do
name "Example category"
user_id 1
end
end
This is my test:
describe "POST #create" do
context "signed in" do
before(:each) do
sign_in :user, user
end
context "with valid attributes" do
it "saves the new category in the database" do
expect{
post :create, FactoryGirl.attributes_for(:category)
}.to change(Category, :count).by(1)
end
it "redirects to category #index"
end
context "with invalid attributes" do
it "does not save the new category in the database"
it "re-renders the :new template"
it "provides errors"
end
end
context "not signed in" do
it "blocks access (somehow)"
end
end
But, I'm getting the following error when I run my test:
3) CategoriesController POST #create signed in with valid attributes saves the new category in the database
Failure/Error: post :create, FactoryGirl.attributes_for(:category)
ActionController::ParameterMissing:
param not found: category
I've managed to use the factory previously, it works fine in my spec/model/ tests. Is there anything I'm doing wrong here? I can't see any problem with the factory. I'm a little lost :(
You have to provide the key:
post :create, category: FactoryGirl.attributes_for(:category)