I am writing controller tests for a Rails application where in users can write reviews about others (but not for themselves).
reviews_controller_spec.rb
describe "PATCH update" do
it "redirects to user_reviews_url" do
@user = FactoryGirl.create :user
sign_in @user
@user1 = FactoryGirl.create :user
@review = FactoryGirl.attributes_for :review
post :create, user_id:@user1.id, review:@review
patch :update, {user_id:@user1.id, id:@review.id}, review: {:description => "john.doeexample1.com"}
flash[:notice].should_not be_nil
flash[:notice].should eq("Review was successfully updated.")
response.should redirect_to(user_path(assigns(:review)))
end
end
factories.rb
FactoryGirl.define do
factory :review do
skill
stars "4"
body "limit: 65535"
end
factory :skill do
name "Cooking"
end
factory :experience do
description "abcdfef"
start_date "1989-11-23"
level "4"
skill
end
factory :user do |u|
u.sequence(:first_name) { |n| "Michael#{n}"}
u.last_name "Harlt"
u.sequence(:email) { |n| "michael#{n}@example.com"}
u.password "foobar123"
u.password_confirmation "foobar123"
u.city "Madison"
u.state "WI"
u.zip_code "53726"
u.date_of_birth "23/11/1989"
end
end
reviews_controller.rb
def update
@skills = @user.skills
if @review.update(review_params)
redirect_to @user, notice: 'Review was successfully updated.'
else
render :edit
end
end
review.rb
class Review < ActiveRecord::Base
has_one :reviewer, class_name: 'User', foreign_key: "reviewer_id"
has_one :reviewee, class_name: 'User', foreign_key: "reviewww_id"
belongs_to :skill
validate :user_cannot_write_review_about_themselves, on: :create
validates_presence_of :stars, :body, :reviewer_id, :reviewee_id, :skill_id
def user_cannot_write_review_about_themselves
if reviewer_id == reviewee_id
errors.add(:reviewer, "cannot write a review about yourself")
end
end
end
This code keeps me giving the error:
Failure/Error: @review = FactoryGirl.create :review
ActiveRecord::RecordInvalid:
Validation failed: Reviewer cannot write a review about yourself, Reviewer can't be blank, Reviewee can't be blank
Please let me know if any other code snippets would also be required.
Aucun commentaire:
Enregistrer un commentaire