Hello: I''m going crazy with a test of a controller action. This is the test describe "POST changetipo" do it "toggle tipo of tije" do tije = FactoryGirl.create(:tije) puts "--------------- #{tije.inspect}--------------" post :changetipo, :id => tije.id puts "+++++++++++++++ #{tije.inspect} +++++++++++++" tije.tipo.should == "2" end end and this is the controller method def changetipo @tije = Tije.find(params[:id]) @tije.tipo == "1" ? @tije.tipo = "2" : @tije.tipo = "1" @tije.save! puts "************* #{@tije.inspect} **************" respond_to do |format| format.html { redirect_to root_path, notice: ''Tije type changed'' } format.json { head :no_content } end end and this is the console output: ---------- #<Tije id: 1, tipo: "1", description: "Hoy" -------------- ********** #<Tije id: 1, tipo: "2", description: "Hoy" ************** ++++++++++ #<Tije id: 1, tipo: "1", description: "Hoy" +++++++++++++ so, at the beginning tipo is 1. After tije.save! is 2, but when it come back to the test, its value is 1 again :( All the other test are working. I only have a problem with this one. I don''t know why this test is failing. If I use web browser to test it it''s working. It changes the value in database. I also have try to change test like this: describe "POST changetipo" do it "toggle tipo of tije" do tije = FactoryGirl.create(:tije) expect { post :changetipo, :id => tije.to_param }.to change(tije.tipo) end end but I have this error Failure/Error: tije.tipo.should == "2" expected: "2" got: "1" (using ==) Can anyone tell me what I''m doing wrong? Thank you very much Raul -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 29 November 2012 11:30, Raul Sanchez <raulxininen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello: > > I''m going crazy with a test of a controller action. > > This is the test > > describe "POST changetipo" do > it "toggle tipo of tije" do > tije = FactoryGirl.create(:tije) > puts "--------------- #{tije.inspect}--------------" > post :changetipo, :id => tije.id > puts "+++++++++++++++ #{tije.inspect} +++++++++++++" > tije.tipo.should == "2" > end > end > > > and this is the controller method > > > def changetipo > @tije = Tije.find(params[:id]) > @tije.tipo == "1" ? @tije.tipo = "2" : @tije.tipo = "1" > @tije.save! > puts "************* #{@tije.inspect} **************" > > respond_to do |format| > format.html { redirect_to root_path, notice: ''Tije type changed'' } > format.json { head :no_content } > end > > end > > > > and this is the console output: > > ---------- #<Tije id: 1, tipo: "1", description: "Hoy" -------------- > ********** #<Tije id: 1, tipo: "2", description: "Hoy" ************** > ++++++++++ #<Tije id: 1, tipo: "1", description: "Hoy" +++++++++++++ > > > so, at the beginning tipo is 1. After tije.save! is 2, but when it come back > to the test, its value is 1 again :(In the test you are creating a variable and then posting a message (passing the id) which changes the value in the database. That does not change the value of the variable in the test. You will have to read it back from the db again to see the changed value. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Ahhhh :-) Thank you. Which will be the right way to do it? El 29/11/2012 12:42, "Colin Law" <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> escribió:> On 29 November 2012 11:30, Raul Sanchez <raulxininen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hello: > > > > I''m going crazy with a test of a controller action. > > > > This is the test > > > > describe "POST changetipo" do > > it "toggle tipo of tije" do > > tije = FactoryGirl.create(:tije) > > puts "--------------- #{tije.inspect}--------------" > > post :changetipo, :id => tije.id > > puts "+++++++++++++++ #{tije.inspect} +++++++++++++" > > tije.tipo.should == "2" > > end > > end > > > > > > and this is the controller method > > > > > > def changetipo > > @tije = Tije.find(params[:id]) > > @tije.tipo == "1" ? @tije.tipo = "2" : @tije.tipo = "1" > > @tije.save! > > puts "************* #{@tije.inspect} **************" > > > > respond_to do |format| > > format.html { redirect_to root_path, notice: ''Tije type > changed'' } > > format.json { head :no_content } > > end > > > > end > > > > > > > > and this is the console output: > > > > ---------- #<Tije id: 1, tipo: "1", description: "Hoy" -------------- > > ********** #<Tije id: 1, tipo: "2", description: "Hoy" ************** > > ++++++++++ #<Tije id: 1, tipo: "1", description: "Hoy" +++++++++++++ > > > > > > so, at the beginning tipo is 1. After tije.save! is 2, but when it come > back > > to the test, its value is 1 again :( > > In the test you are creating a variable and then posting a message > (passing the id) which changes the value in the database. That does > not change the value of the variable in the test. You will have to > read it back from the db again to see the changed value. > > Colin > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
On 29 November 2012 11:58, Raul Sanchez <raulxininen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ahhhh :-) > > Thank you. > > Which will be the right way to do it?Just fetch it from the db again before tesating it Colin> > El 29/11/2012 12:42, "Colin Law" <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> escribió: >> >> On 29 November 2012 11:30, Raul Sanchez <raulxininen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > Hello: >> > >> > I''m going crazy with a test of a controller action. >> > >> > This is the test >> > >> > describe "POST changetipo" do >> > it "toggle tipo of tije" do >> > tije = FactoryGirl.create(:tije) >> > puts "--------------- #{tije.inspect}--------------" >> > post :changetipo, :id => tije.id >> > puts "+++++++++++++++ #{tije.inspect} +++++++++++++" >> > tije.tipo.should == "2" >> > end >> > end >> > >> > >> > and this is the controller method >> > >> > >> > def changetipo >> > @tije = Tije.find(params[:id]) >> > @tije.tipo == "1" ? @tije.tipo = "2" : @tije.tipo = "1" >> > @tije.save! >> > puts "************* #{@tije.inspect} **************" >> > >> > respond_to do |format| >> > format.html { redirect_to root_path, notice: ''Tije type >> > changed'' } >> > format.json { head :no_content } >> > end >> > >> > end >> > >> > >> > >> > and this is the console output: >> > >> > ---------- #<Tije id: 1, tipo: "1", description: "Hoy" -------------- >> > ********** #<Tije id: 1, tipo: "2", description: "Hoy" ************** >> > ++++++++++ #<Tije id: 1, tipo: "1", description: "Hoy" +++++++++++++ >> > >> > >> > so, at the beginning tipo is 1. After tije.save! is 2, but when it come >> > back >> > to the test, its value is 1 again :( >> >> In the test you are creating a variable and then posting a message >> (passing the id) which changes the value in the database. That does >> not change the value of the variable in the test. You will have to >> read it back from the db again to see the changed value. >> >> Colin >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit https://groups.google.com/groups/opt_out. >> >> > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Done and green :) Thank you again 2012/11/29 Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>> On 29 November 2012 11:58, Raul Sanchez <raulxininen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Ahhhh :-) > > > > Thank you. > > > > Which will be the right way to do it? > > Just fetch it from the db again before tesating it > > Colin > > > > > El 29/11/2012 12:42, "Colin Law" <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> escribió: > >> > >> On 29 November 2012 11:30, Raul Sanchez <raulxininen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> > Hello: > >> > > >> > I''m going crazy with a test of a controller action. > >> > > >> > This is the test > >> > > >> > describe "POST changetipo" do > >> > it "toggle tipo of tije" do > >> > tije = FactoryGirl.create(:tije) > >> > puts "--------------- #{tije.inspect}--------------" > >> > post :changetipo, :id => tije.id > >> > puts "+++++++++++++++ #{tije.inspect} +++++++++++++" > >> > tije.tipo.should == "2" > >> > end > >> > end > >> > > >> > > >> > and this is the controller method > >> > > >> > > >> > def changetipo > >> > @tije = Tije.find(params[:id]) > >> > @tije.tipo == "1" ? @tije.tipo = "2" : @tije.tipo = "1" > >> > @tije.save! > >> > puts "************* #{@tije.inspect} **************" > >> > > >> > respond_to do |format| > >> > format.html { redirect_to root_path, notice: ''Tije type > >> > changed'' } > >> > format.json { head :no_content } > >> > end > >> > > >> > end > >> > > >> > > >> > > >> > and this is the console output: > >> > > >> > ---------- #<Tije id: 1, tipo: "1", description: "Hoy" -------------- > >> > ********** #<Tije id: 1, tipo: "2", description: "Hoy" ************** > >> > ++++++++++ #<Tije id: 1, tipo: "1", description: "Hoy" +++++++++++++ > >> > > >> > > >> > so, at the beginning tipo is 1. After tije.save! is 2, but when it > come > >> > back > >> > to the test, its value is 1 again :( > >> > >> In the test you are creating a variable and then posting a message > >> (passing the id) which changes the value in the database. That does > >> not change the value of the variable in the test. You will have to > >> read it back from the db again to see the changed value. > >> > >> Colin > >> > >> -- > >> You received this message because you are subscribed to the Google > Groups > >> "Ruby on Rails: Talk" group. > >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org. > >> To unsubscribe from this group, send email to > >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> For more options, visit https://groups.google.com/groups/opt_out. > >> > >> > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.