I use FactoryGirl in my tests, but the mocks and stubs are present as well (mock_model(User).as_null_object).
There is a strange behaviour: sometimes FactoryGirl can't create a record, because instead of normal date it inserts '#[User:0x827966c #name="User_1132"]' string into created_at and updated_at.
Any ideas why?
Related
I used assert_template and assigns extensively before Rails 5 in my controllers tests. For example to test that a link in the view is correct:
assert_select ".left-off-canvas-menu ul li a[href=?]", event_all_items_path(assigns(:event)), "Back to all items"
I figured that assert_template was not difficult to get rid of.
However, I don't know how to replace test cases like the one above.
I know about the gem to include those functions, but I'd like to try without them. So alternatives would be:
Would you make a case to keep the assigns function or would you say it is meant to be hard coded?
I'm using the Linkedin gem to pull profile information for RoR 3.
Gem: https://github.com/pengwynn/linkedin
API Doc: https://developer.linkedin.com/documents/profile-fields#positions
Everything works except when I get to a property with a dash in the name.
<%=position.title %> displays correctly but<%= position.start-date %> return a NoMethodError in Users#show - undefined method start.
I've tried different operations like "startDate", "start_date", quotes around "start-date" but none have worked.
Is there a proper way to escape the dash/hyphen in the property name?
The expression in your ERB will be parsed as subtracting the value of the date variable from the result of a call to the start() method of the position object. Hyphens aren't valid in identifiers within Ruby.
I'm not familiar enough with the LinkedIn gem to suggest a solution, except to say that since it's based on an XML API, you should look for a way to manually pull data out of a tag pair. Most similar gems offer such a method. Also, this is a great case for using IRB as an exploratory tool: fire up an IRB session and see what happens when you call position.methods, after properly creating the position variable of course. My guess would be that you'll see something in that list which suggests an answer.
Looks like it returns a Hashie::Mash which converts keys, with a few extra rules:
https://github.com/pengwynn/linkedin/blob/master/lib/linked_in/mash.rb
You said you'd already tried position.start_date right? That should work. But if not, have you tried position['start-date'] or position['start_date'] one of those two should also work, since it's a Mash.
I am running into problems passing a hash (serialized as a hash) to a newly created object. The values get converted to yaml format.
Consider the following:
Model ComputerUser:
...
serialize preferences
'#in the database I see the following "{0=>{color:red format:html}}"
....
#computer_user.registrations.build(:user_pref => :preferences}.save
#computer_user.user_pref;
'#the above statement spills out the data in yaml format and that is how it gets persisted in the db.
Now, if I do the following from rails console, I don't see the same issue, i.e. the hash is stored as a hash and not converted to the yaml format. I see the following when I inspect the value of the column in the new object:
=>{0=>{color:red format:html}}
Please note that I have used serialize for the attributes in the source as well as the target. Things seem to work from the console but just note from the controller! Any ideas what is going on? Why is the issue occurring only in the web application and not on the console.
The issue was that I was not using instance variables for assignment.
If you have "serialize, Hash" declared in both classes, this is how you need to build the child object from the parent:
#computer_user.registrations.build(:user_prefs => #computer_user.preferences).save
That works like a charm.
I have a Moderator model which basically queries web site related stat results from other models.
An e.g. of displayed stats could be the total number of users belonging to a particular area out of many areas in a city. Limiting the number of such records to a fixed number. For this, the body defined within the Moderator model makes use of an Area model.
Since the queries are not using anything from the same model, but actually from other models, there wasn't a need for me to have a table migration wrt this model.
I basically am now trying to test the defined methods in the Moderator model using Rspec.
What I am basically trying to test is that a call to the method should return success, this I am doing through:-
subject.send(:method_name)
response.should be_success
I'm getting a common error for all such defined methods saying that database_name.table_name does not exist. Well , this is true but how should is it really making a difference?, and how to get a work around for this to just test a simple use case of calling a method successfully in the above context.
Thanks.
Try something like this
describe Moderator do
it "should do something" do
moderator = Moderator.new
moderator.something.should == "do"
end
end
This assumes that you have a method something on Moderator and that it returns the string "do".
I think the RSpec Book provides great information and examples if you want to get more in-depth with your rspec.
Well,
The below line code did work for me:-
Model_name.method_name.should_not be_nil
Did someone try to integrate puret into rails_admin? I can't make a language switch to edit different translations :(
Changing I18n.locale forces whole rails_admin to use specified locale.
Now I got the solution. The two can work together well. In short:
Delete the pureted column(s) in your model
If you have the column pureted still in your model, rails form helper will bypass puret. That is, if a model called Post has a field called contents to be i18ned, the table posts SHOULD NOT have the column contents.
Actually we should use globalize3 instead. With this you do not need to remove the original column. And puret doens't support nested attributes assignment. globalize3 works very well.